0

I created this method, but when i try to call the method it returns a null pointer exception. Can anybody tell me what's wrong? help me please

public Talent SearchTalentQuery(int talentID){
    String SearchTalentString = "SELECT * FROM TALENT WHERE TALENTID = "+talentID+";";
    Talent talent=null;
    try{
        //error in select query
        if(getOracleXEConnection()!=null){

            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(SearchTalentString);                       
                   while( rs.next()){
                   talent.setTalentName(rs.getString("talentname"));
                   talent.setTalentFocus(rs.getString("maintalentfocus"));
                   talent.setTalentNationality((rs.getString("nationality")));
                   talent.setTalentGroup(rs.getString("talentgroup"));
                   talent.setTalentHiredate(rs.getString("hiredate"));
                   talent.setTalentAge(Integer.parseInt(rs.getString("age")));
                   talent.setTalentID(Integer.parseInt(rs.getString("talentid")));
                   }

            rs.close();
            statement.close();
            conn.close();


            return talent;
        }
        else{
            return null;
        }
    }
    catch(SQLException | NumberFormatException e){
        e.printStackTrace();
    }
    return null;

6 Answers 6

5

You're not instantiating a Talent object to assign to.

Talent talent=null;
...
talent.setTalentName(...

To address your comment, you're initialising the talent reference to a null pointer. You need to instantiate an object and point to it in order to call methods on it.

Depending on your constraints etc. your method will likely want to return this Talent object, or create a new one through each iteration and return a collection of these.

Sign up to request clarification or add additional context in comments.

2 Comments

isn't is initialized on this statement: Talent talent=null; ?
Talent talent=null This is to null, no object, how can you call method on it? it is null. This is what NullPointerException called
3

Just instantiate your talent object at the beginning of the while block

...

while( rs.next()){ 
 talent = new Talent(); //or whatever
 talent.setTalentName(rs.getString("talentname"));

...

Comments

2

Because your whole code does not initialize memory for talent object. you need to initialize memory for talent object.

use: Talent talent= new Talent(); at the place of Talent talent = null;

It might solve your problem.

Comments

1

You need to instantiate talent, but also you should really use PreparedStatement as this code is extremely open to SQL injections...

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Finally, for convention, searchTalentString should start with a lower case character (and maybe be made final)...

String searchTalentString = "SELECT * FROM TALENT WHERE TALENTID = "+talentID+";";

Comments

0

Change to

String SearchTalentString = "SELECT * FROM TALENT WHERE TALENTID = "+talentID+";";
Talent talent=new Talent();

Or something like this.

Comments

0

Example

   public void Insert(){
            try {
                Statement sta = conn.createStatement();
                Statement consulta = conn.createStatement();
                String query = "SELECT * FROM coches";
                System.out.println("Fecha de entrada");
                String fe = scanner.next();
                System.out.println("Fecha de salida");
                String fs = scanner.next();
                
                ResultSet rs = consulta.executeQuery(query);
                int i = 1;
                while (rs.next()) {
                    System.out.println(i + "," + rs.getString("matricula") + "," + rs.getString("marca") + "," + rs.getString("modelo")); 
                    i++;
                }
                System.out.println("Selecciona el numero del coche");
                int n = scanner.nextInt();
                rs.absolute(n);
               
                
                sta.executeUpdate("INSERT INTO reparaciones (fecha_entrada,fecha_salida,coche) VALUES ('"+ fe + "', '" + fs + "', '" + rs.getString("matricula") + "');");
                
                System.out.println("Reparación insertada, indica los materiales usados");           
               
                String nombre = "";
                
                do{
                    System.out.println("-------------------------------------------------");
                    System.out.println("Nombre :");
                    scanner.nextLine();
                    nombre = scanner.nextLine();                           
                    if(!nombre.equals("")){
                        System.out.println("Cantidad :");
                        int cant = scanner.nextInt();
                        System.out.println("Precio :");
                        int prec = scanner.nextInt();
                        sta.executeUpdate("INSERT INTO materiales (nombre,cantidad,precio,reparacion) VALUES ('"+ nombre + "', '" + cant + "', '" + prec +  "', '" + n + "');");
                    }
               } 
               while(!nombre.equals(""));   
                sta.close();
                rs.close();
                consulta.close();
            } catch (SQLException ex) {
                System.out.println("ERROR:al hacer un Insert");         
                ex.printStackTrace();
            }
        }
        public void Delete(){
            try {
                Statement baja = conn.createStatement();
                Statement consulta = conn.createStatement();
                String query = "SELECT * FROM materiales";
                ResultSet rs = consulta.executeQuery(query);
                int i = 1;
                while (rs.next()) {
                    System.out.println(rs.getInt("id") + "," + rs.getString("nombre"));
                    i++;
                }
                System.out.println("Elige una opcion");
                int opc = scanner.nextInt();
               
                String query2 = "SELECT reparacion FROM materiales WHERE id = " + opc;
                ResultSet rs2 = consulta.executeQuery(query2);
                if(rs2.next()){
                  if(rs2.getInt("reparacion")!=0){
                    System.out.println("No es posible darlo de baja");
                  }
                  else{
                    String queryBaja = "DELETE FROM materiales WHERE id ='" + opc +"'";
                    baja.executeUpdate(queryBaja);
                    System.out.println("Eliminado con exito");  
                  }
                }
    
                
                baja.close();
                consulta.close();
                rs.close();
                rs2.close();
            } catch (SQLException ex) {
                System.out.println("ERROR:al hacer un delete");         
                ex.printStackTrace();           
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.