Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

NullPointerException

  Asked By: Navin    Date: May 06    Category: Java    Views: 524
  

I am building an elevator simulation for an assignment.



I have the following preliminary driver class:



import java.util.*;

public class Driver
{
_public static void main (String args[])
__{
__int newUpFloor;
__Elevator theElevator = new Elevator();

__newUpFloor = 4;
__theElevator.insertUp(newUpFloor);
__System.out.println(theElevator.getFirstFloor());
_}
}



then I have the following preliminary elevator class:



import java.util.*;

public class Elevator
{
_private LinkedList upList;
_public Elevator()
_{
__LinkedList upList = new LinkedList();

_}

_public void insertUp (int floor)
_{
__Integer theFloor;
__theFloor = new Integer(floor);
//__System.out.println (theFloor);
__Integer anotherFloor;
__anotherFloor = new Integer(0);
__int firstElement;
__
__upList.add((Integer)theFloor);

__anotherFloor = (Integer)upList.get(0);
__firstElement = anotherFloor.intValue();
_}

_public int getFirstFloor()
_{
__Integer ObjectFirstFloor = (Integer) upList.get(0);
__int firstFloor = ObjectFirstFloor.intValue();
__return firstFloor;
_}
}





I simply want to build the LinkedList component at this stage. Both compile
fine, but when I try to run it (hoping for simply a “4” as output) I get a
NullPointerException.



Ïjava.lang.NullPointerException
ÏϧÏ_at Elevator.insertUp(Elevator.java:28)
ÏϧÏ_at Driver.main(Driver.java:11)
ÏϧÏException in thread "main"





I have torn half my hair out and am at a loss. Any thoughts as to what I
am missing would be greatly appreciated.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Rocco Anderson     Answered On: May 06

nowhere have you inserted floor 0 into upList,

anotherFloor = (Integer)upList.get(0);
firstElement = anotherFloor.intValue();

so anotherFloor will be null
and anotherFloor.intValue() will throw a null pointer exception
nothing strange here (except perhaps your expectation that it won't)

 
Answer #2    Answered By: Scott Simmons     Answered On: May 06

But I do add it..

upList.add((Integer)theFloor appears just before the code you quote.

Any comments or thoughts?

 
Answer #3    Answered By: Raju Srinivas     Answered On: May 06

No, get(0) returns the first item in the list.

The problem is in your Elevator constructor. You never initialize
upList. You create a new local upList that gets destroyed when the
constructor returns.

Also, you do not have to cast theFloor when you add it to the list.

 
Answer #4    Answered By: Neil Turner     Answered On: May 06


U have declared a list inside a constructor whose scope ends at the closing
brace of the constructor,
uplist pointing to nothing ie null. put the reference out of the constructor
and that would work I suppose. Please let me know if it works

 
Answer #5    Answered By: Katrina Edwards     Answered On: May 06

Your Code in the constructor should be re-written as
public class  Elevator
{
private LinkedList upList;
public Elevator()
{
//LinkedList upList = new LinkedList(); // This existed before the mod
upList = new LinkedList(); // This is the modified line uplist has been
declared initialize it
}

...............

}

 
Answer #6    Answered By: Eddie Austin     Answered On: May 06

The problem is in ur Elevator Constructor
Here is the corrected code

import java.util.*;

public class  Elevator
{
private LinkedList upList;
public Elevator()
{
upList = new LinkedList();
}

public void  insertUp (int floor)
{
Integer theFloor;
theFloor = new Integer(floor);
//System.out.println (theFloor);
Integer anotherFloor;
anotherFloor = new Integer(0);
int firstElement;
upList.add((Integer)theFloor);
anotherFloor = (Integer)upList.get(0);
firstElement = anotherFloor.intValue();
}

public int  getFirstFloor()
{
Integer ObjectFirstFloor =
(Integer)upList.get(0);
int firstFloor = ObjectFirstFloor.intValue();
return firstFloor;
}

 
Answer #7    Answered By: Antonio Dunn     Answered On: May 06

Please remove the Linked List in teh constructor.

_public Elevator()
_{
__LinkedList upList = new LinkedList();

_}

In the constructor instead of
__LinkedList upList = new LinkedList();

use
upList = new LinkedList();

 
Didn't find what you were looking for? Find more on NullPointerException Or get search suggestion and latest updates.