Java Error NullPointerException in construction

-1

main.java

package projeto;

import java.sql.*;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class projetoDB {
     static final String ConnectionURL = "jdbc:sqlserver://127.0.0.1:1433;" + "databaseName=projetoDB;integratedSecurity=true;";

    public static void main(String[] args){       
        String User = "dema";
        String Password = "123456";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try{

            stmt = con.prepareStatement("SELECT nomemed FROM medicos WHERE crm=''");            
            ResultSetMetaData colunas = rs.getMetaData();
            int numeroColunas = colunas.getColumnCount();

            while (rs.next()){
                for (int i=1; i<=numeroColunas; i++)
                JOptionPane.showMessageDialog(null, "dados do médico " + rs.getObject(i));
            }


            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(ConnectionURL, User, Password);
            System.out.println("Conexão Obtida com Sucesso!/n");

            String SQL = "SELECT nomemed FROM medicos WHERE crm=''";

            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            System.out.println("informações do Banco de Dados");

            for (int i=1; i<=numeroColunas; i++)
            System.out.println(colunas.getColumnName(i));

            while (rs.next()){

                for (int i=1; i<=numeroColunas; i++)
                    System.out.println(rs.getObject(i));
                    System.out.println("--------------------------------");
            }
        }

        catch(Exception e){
            e.printStackTrace();
        }

        finally{

            try{

                rs.close();
                stmt.close();
                con.close();
            }
            catch (Exception erro){

                erro.printStackTrace();
            }
        }    
    }

    void buscar(int crm) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

medicos.java

package projeto;

public class Medicos {

    private int crm;
    private String nomemed;
    private String especialidademed;

    public Medicos (int crm, String nomemed, String especialidademed){

        this.crm = crm;
        this.nomemed = nomemed;
        this.especialidademed = especialidademed;
    }

    public int getCrm(){
        return crm;
    }

    public void setCrm(int crm){
        this.crm = crm;
    }

    public String getNomemed(){
        return nomemed;
    }

    public void setNomemed(String nomemed){
        this.nomemed = nomemed;
    }

    public String getEspecialidademed(){
        return especialidademed;
    }

    public void setEspecialidademed(String especialidademed){
        this.especialidademed = especialidademed;
    }

    @Override
    public String toString(){
        return "medicos{" + "crm=" + crm + ", nomemed=" + nomemed + ", especialidademed=" + especialidademed + '}';
    }    
}

Error is here!

run:
java.lang.NullPointerException
    at projeto.projetoDB.main(projetoDB.java:26)
java.lang.NullPointerException
    at projeto.projetoDB.main(projetoDB.java:65)
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)**

How do I solve this? Already appeared several errors, I was solving little by little, but I could not understand anymore ..

    
asked by anonymous 20.05.2018 / 22:27

1 answer

1

Your error is here:

stmt = con.prepareStatement("SELECT nomemed FROM medicos WHERE crm=''"); 

The variable con has been declared here:

Connection con = null;

The problem is that it has never been initialized, so when you tried to access the prepareStatement() method on a null object, the program launched a NullPointerException .

Another problem is this line here:

ResultSetMetaData colunas = rs.getMetaData();

The variable rs has been declared, but not initialized, so this line will also throw a NullPointerException .

21.05.2018 / 00:20