Java Nullpointerexception error in construction

Asked

Viewed 46 times

-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.
    }
}

java doctors.

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 this one!

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 ? There have been several mistakes, I was solving little by little, but I could not understand more..

  • Where are lines 26 and 65 highlighted in error?

  • If your main class is just that, you have an error in it, Voce uses the connection and hasn’t even created an instance yet.

1 answer

1


Your mistake is here:

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

The variable con was declared here:

Connection con = null;

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

Another problem is this line here:

ResultSetMetaData colunas = rs.getMetaData();

The variable rs was declared, but not initialized, so this line will also launch a NullPointerException.


Browser other questions tagged

You are not signed in. Login or sign up in order to post.