Home arrow static arrow Java Programming [Archive] - java.lang.NullPointer
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java.lang.NullPointer
This topic has 6 replies on 1 page.

Posts:65
Registered: 4/9/04
java.lang.NullPointer  
Jul 20, 2004 7:22 AM



 
Hi!!!!!!!!!!!!

JBuilder advises me of a java.lang.NullPointerException and i don't understand why.
This code is inside a class that extends TimerTask (a timer):

JListaTranServers listaTranServers = new JListaTranServers();if (lineaEntrada.equals(OK)){    System.out.println("Tran Server que escucha en el puerto: " + listaPuertos.getItem(i).getIdentificador());    System.out.println("Tran Server de la plaza             : " + listaPuertos.getItem(i).getDescripcion());    System.out.println("Estado:                             : " + OK);     JMemoriaTranServers memoria = new JMemoriaTranServers();    memoria.setDescripcion(listaPuertos.getItem(i).getDescripcion());    memoria.setIdentificador(listaPuertos.getItem(i).getIdentificador());    memoria.setEstado(OK);    listaTranServers.agregarItem(memoria); // the problem is here     // agregarItem(memoria) does this = "listaTranServers.addElement(memoria);"}


What could be going on????

Bye!!!!!!!!
 

Posts:65
Registered: 4/9/04
Re: java.lang.NullPointer  
Jul 20, 2004 7:31 AM (reply 1 of 6)



 
I forgot something:
listaTranServers is an instance of a class that uses the Vector class.

Bye
 

Posts:21,719
Registered: 98-02-20
Re: java.lang.NullPointer  
Jul 20, 2004 7:31 AM (reply 2 of 6)



 
I'm betting that you didn't allocate memory for the Vector listaTranServers inside your JListaTranServers constructor. You declared the variable name and type, but you never allocated memory to it so the reference is still null.

If so, learn how to write a constructor properly.

If you compile with the debug option -g turned on the JVM will spit out the exact line number where the exception was thrown in the stack trace. It's worth knowing. You'll be able to spot errors much faster that way, especially for easy ones like NullPointerException.

%
 

Posts:24,036
Registered: 2/3/03
Re: java.lang.NullPointer  
Jul 20, 2004 7:32 AM (reply 3 of 6)



 
// agregarItem(memoria) does this = "listaTranServers.addElement(memoria);"

I'll just bet that's not all it does. Look inside that method for potential null references, or post the agregarItem() method here.
 

Posts:21,719
Registered: 98-02-20
Re: java.lang.NullPointer  
Jul 20, 2004 7:32 AM (reply 4 of 6)



 
You need to add a line like this:

private Vector listaTranServers = new Vector();


%

PS - Why Vector? java.util.ArrayList is preferred these days.
 

Posts:65
Registered: 4/9/04
Re: java.lang.NullPointer  
Jul 20, 2004 7:35 AM (reply 5 of 6)



 
Hi again

Thanks but i was doing another thing. I did reserved memory in the constructor but i made a mistake in another place

It's done

Thanks

Bye!!!!!!!!!!!!
 

Posts:24,036
Registered: 2/3/03
Re: java.lang.NullPointer  
Jul 20, 2004 7:35 AM (reply 6 of 6)



 
You need to add a line like this:

Only if listaTranServers actually extends Vector. If it just "uses" (i.e. has-a) Vector, make sure the Vector is initialized properly, per duffy's comments.

I second the motion about not using Vectors, though...
 
This topic has 6 replies on 1 page.