Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

NullPointerException error???

mdw1982mdw1982 Member Posts: 124
Hi all,

I'm still relatively new to programming with linked lists and I've got something that I've been working on a few days that I could use some help with. below is the main method to my program and when I attempt to run it I'm getting a NullPointerException error from the compiler.

The program compiles ok, and it starts as it should. The first thing to happen is an input dialog pops up, I enter an integer. ( this is to control the size of the list ). Next, a menu appears, (another input dialog ), MenuItem(1) = displayList. MenuItem(2-4) dont' work cause the propram isn't finished yet, however, the last item on the menu terminates the program and it works just fine. When I try to use MenuItem(1) though, the program blows up with the NullPointerException.

I know [b]what[/b] that means, and I realize this is a logic error on my part, but I can't quite see what I'm doing wrong. need a few more brain cells and another pair of eyes.

I'm attempting to automate the creation of data nodes in a linked list, but I'm getting stuck somewhere.

Tron: I took a look at Vectors and decided for now to stick with the LinkedList instead since for now that makes a little more sense to me.

[code]

public static void main(String[] args){
// setting up the Linked List
IntNode head = null;
IntNode curr = null;
IntNode newNode = null;
int count = 0;

int numNode = getUserInput("How many Nodes are we making?");

head = curr;
while ( count <= numNode ){
newNode = new IntNode(getNum());
[red]curr.setNext(newNode);[/red] // <--- according to the compiler
count++; // this is where the program
// is running into trouble
}

head = newNode;

interActiveMenu(head, curr);

System.exit(0);
}

[blue]
Runtime Error Message:
Exception in thread "main" java.lang.NullPointerException
at LLSort.main(LLSort.java:48)
[/blue]

[/code]

Thanks,

Mark

Comments

  • : Hi all,
    :
    : I'm still relatively new to programming with linked lists and I've got something that I've been working on a few days that I could use some help with. below is the main method to my program and when I attempt to run it I'm getting a NullPointerException error from the compiler.
    :
    : The program compiles ok, and it starts as it should. The first thing to happen is an input dialog pops up, I enter an integer. ( this is to control the size of the list ). Next, a menu appears, (another input dialog ), MenuItem(1) = displayList. MenuItem(2-4) dont' work cause the propram isn't finished yet, however, the last item on the menu terminates the program and it works just fine. When I try to use MenuItem(1) though, the program blows up with the NullPointerException.
    :
    : I know [b]what[/b] that means, and I realize this is a logic error on my part, but I can't quite see what I'm doing wrong. need a few more brain cells and another pair of eyes.
    :
    : I'm attempting to automate the creation of data nodes in a linked list, but I'm getting stuck somewhere.
    :
    : Tron: I took a look at Vectors and decided for now to stick with the LinkedList instead since for now that makes a little more sense to me.
    :
    : [code]
    :
    : public static void main(String[] args){
    : // setting up the Linked List
    : IntNode head = null;
    : IntNode curr = null;
    : IntNode newNode = null;
    : int count = 0;
    :
    : int numNode = getUserInput("How many Nodes are we making?");
    :
    : head = curr;
    : while ( count <= numNode ){
    : newNode = new IntNode(getNum());
    : [red]curr.setNext(newNode);[/red] // <--- according to the compiler
    : count++; // this is where the program
    : // is running into trouble
    : }
    :
    : head = newNode;
    :
    : interActiveMenu(head, curr);
    :
    : System.exit(0);
    : }
    :
    : [blue]
    : Runtime Error Message:
    : Exception in thread "main" java.lang.NullPointerException
    : at LLSort.main(LLSort.java:48)
    : [/blue]
    :
    : [/code]
    :
    : Thanks,
    :
    : Mark
    :
    :

    "curr" is null at program start, this causes the NullPointerException to raise. You can set head and curr before the loop, or add an if-statement inside the loop like that:

    [code]
    //!head = curr; leave this out

    while ( count < numNode ) { //!it's < not <=
    newNode = new IntNode(getNum());
    count++;

    if (curr == null) {
    curr = newNode;
    head = newNode;
    } else curr.setNext(newNode);
    }

    //!head = newNode; leave this out
    [/code]

    tron.
  • mdw1982mdw1982 Member Posts: 124
    Hi Tron,

    Thanks for the info. It sorta works now, but strangely. There is a line I placed in the main method to afford me some debugging. So, if you wouldn't mind, give this a run and watch what it's doing.

    I've found that if I comment out the if/else statement and place [b]curr = newNode;[/b] in thd program like this:

    [code]
    [red]
    newNode = new IntNode(getNum());
    count++;[/red]
    [blue]curr = newNode;[/blue]

    [/code]

    then the list display panel is empty, however, the console shows ten random integers have been generated.

    [b]if[/b] I leave the if/else statement in tact then in the console all the numbers that appear are the same, and the list display panel displays only two integer values. They're both different values, but still there are only two of them.

    I'm not sure, but I think I'm even more perplexed.

    [code]

    import javax.swing.JOptionPane;
    class IntNode{

    private int item;
    private IntNode next;

    public IntNode( int newItem ){
    item = newItem;
    next = null;
    }

    public IntNode( int newItem, IntNode nextNode ){
    item = newItem;
    next = nextNode;
    }

    public void setItem( int newItem ){
    item = newItem;
    }

    public int getItem(){
    return item;
    }

    public void setNext(IntNode nextNode){
    next = nextNode;
    }

    public IntNode getNext(){
    return next;
    }
    }
    class LLSort{

    public static void main(String[] args){
    // setting up the Linked List
    IntNode head = null;
    IntNode curr = null;
    IntNode newNode = null;
    int count = 0;

    int numNode = getUserInput("How many Nodes are we making?");

    while ( count < numNode ){
    newNode = new IntNode(getNum());
    count++;
    if ( curr == null ){
    curr = newNode;
    head = newNode;
    }
    else{
    curr.setNext(newNode);
    System.out.println(curr.getItem() + "
    ");
    }
    //System.out.println(count + "
    ");
    }
    interActiveMenu(head, curr);

    System.exit(0);
    }

    public static void interActiveMenu(IntNode head, IntNode curr){
    int menuItem = 0;
    do {
    String mainMenu = buildMenu();

    menuItem = getMenuItem(mainMenu);

    startMenu(menuItem, head, curr);
    }while( menuItem != 0 );
    }

    public static int getMenuItem(String mainMenu){
    String theItemStr = JOptionPane.showInputDialog(mainMenu);
    int theItem = new Integer(theItemStr.trim()).intValue();
    return theItem;
    }

    public static void startMenu(int menuItem, IntNode head, IntNode curr){
    final int DISPLAY = 1;
    final int SORT = 2;
    final int DELETE = 3;
    final int INSERT = 4;
    final int QUIT = 0;
    switch ( menuItem ){
    // display unsorted Linked List
    case DISPLAY: displayList(head, curr);
    break;
    // display sorted Linked List
    case SORT: displaySortedList();
    break;
    // delete nodes and display results
    case DELETE: deleteListItems();
    break;
    // insert items into Linked List
    case INSERT: insertItems();
    break;
    // terminate program when 'Q' is entered
    case QUIT: displayExitMsg("Terminating Program: Good-bye!");
    break;
    default: break;
    }
    }

    public static void displayExitMsg(String message){
    System.out.println(message);
    System.exit(0);
    }

    public static void insertItems(){
    }

    public static void deleteListItems(){
    }

    public static void displaySortedList(){
    }

    public static String buildMenu(){
    String menuOutput = "
    Linked List Menu";
    menuOutput += "
    To display part A of Random Integers enter 1 ";
    menuOutput += "
    To Display part B of Random Integers enter 2 ";
    menuOutput += "
    To display part C of Random Integers enter 3 ";
    menuOutput += "
    To display part D of Random Integers enter 4 ";
    menuOutput += "
    To terminate this program of Random Interger enter 0 ";
    return menuOutput;
    }

    public static int getNum(){
    double theSeed = Math.random();
    int newNum = (int)(Math.round(theSeed * 100) );
    return newNum;
    }

    // display of unsorted list
    public static void displayList(IntNode head, IntNode curr){
    String output = "";
    for(curr = head; curr != null; curr = curr.getNext())
    output += " " + curr.getItem();
    JOptionPane.showMessageDialog(null, output, "Linked List Display",
    JOptionPane.INFORMATION_MESSAGE);
    }

    public static int getUserInput(String prompt){
    String inputStr = JOptionPane.showInputDialog(prompt);
    int input = new Integer(inputStr.trim()).intValue();
    return input;
    }

    } // end of class LLSort


    [/code]
  • Yeah, I did an error in my post - sorry :-(
    This works now:

    [code]
    while (count < numNode) {
    newNode = new IntNode(getNum());
    count++;

    //if the head is not initialized, then do it now
    if ( head == null ) head = newNode;
    //else add newNode to last one
    else curr.setNext(newNode);

    //remember newNode as the last one
    curr = newNode;
    }
    [/code]

    tron.

    : Hi Tron,
    :
    : Thanks for the info. It sorta works now, but strangely. There is a line I placed in the main method to afford me some debugging. So, if you wouldn't mind, give this a run and watch what it's doing.
    :
    : I've found that if I comment out the if/else statement and place [b]curr = newNode;[/b] in thd program like this:
    :
    : [code]
    : [red]
    : newNode = new IntNode(getNum());
    : count++;[/red]
    : [blue]curr = newNode;[/blue]
    :
    : [/code]
    :
    : then the list display panel is empty, however, the console shows ten random integers have been generated.
    :
    : [b]if[/b] I leave the if/else statement in tact then in the console all the numbers that appear are the same, and the list display panel displays only two integer values. They're both different values, but still there are only two of them.
    :
    : I'm not sure, but I think I'm even more perplexed.
    :
    : [code]
    :
    : import javax.swing.JOptionPane;
    : class IntNode{
    :
    : private int item;
    : private IntNode next;
    :
    : public IntNode( int newItem ){
    : item = newItem;
    : next = null;
    : }
    :
    : public IntNode( int newItem, IntNode nextNode ){
    : item = newItem;
    : next = nextNode;
    : }
    :
    : public void setItem( int newItem ){
    : item = newItem;
    : }
    :
    : public int getItem(){
    : return item;
    : }
    :
    : public void setNext(IntNode nextNode){
    : next = nextNode;
    : }
    :
    : public IntNode getNext(){
    : return next;
    : }
    : }
    : class LLSort{
    :
    : public static void main(String[] args){
    : // setting up the Linked List
    : IntNode head = null;
    : IntNode curr = null;
    : IntNode newNode = null;
    : int count = 0;
    :
    : int numNode = getUserInput("How many Nodes are we making?");
    :
    : while ( count < numNode ){
    : newNode = new IntNode(getNum());
    : count++;
    : if ( curr == null ){
    : curr = newNode;
    : head = newNode;
    : }
    : else{
    : curr.setNext(newNode);
    : System.out.println(curr.getItem() + "
    ");
    : }
    : //System.out.println(count + "
    ");
    : }
    : interActiveMenu(head, curr);
    :
    : System.exit(0);
    : }
    :
    : public static void interActiveMenu(IntNode head, IntNode curr){
    : int menuItem = 0;
    : do {
    : String mainMenu = buildMenu();
    :
    : menuItem = getMenuItem(mainMenu);
    :
    : startMenu(menuItem, head, curr);
    : }while( menuItem != 0 );
    : }
    :
    : public static int getMenuItem(String mainMenu){
    : String theItemStr = JOptionPane.showInputDialog(mainMenu);
    : int theItem = new Integer(theItemStr.trim()).intValue();
    : return theItem;
    : }
    :
    : public static void startMenu(int menuItem, IntNode head, IntNode curr){
    : final int DISPLAY = 1;
    : final int SORT = 2;
    : final int DELETE = 3;
    : final int INSERT = 4;
    : final int QUIT = 0;
    : switch ( menuItem ){
    : // display unsorted Linked List
    : case DISPLAY: displayList(head, curr);
    : break;
    : // display sorted Linked List
    : case SORT: displaySortedList();
    : break;
    : // delete nodes and display results
    : case DELETE: deleteListItems();
    : break;
    : // insert items into Linked List
    : case INSERT: insertItems();
    : break;
    : // terminate program when 'Q' is entered
    : case QUIT: displayExitMsg("Terminating Program: Good-bye!");
    : break;
    : default: break;
    : }
    : }
    :
    : public static void displayExitMsg(String message){
    : System.out.println(message);
    : System.exit(0);
    : }
    :
    : public static void insertItems(){
    : }
    :
    : public static void deleteListItems(){
    : }
    :
    : public static void displaySortedList(){
    : }
    :
    : public static String buildMenu(){
    : String menuOutput = "
    Linked List Menu";
    : menuOutput += "
    To display part A of Random Integers enter 1 ";
    : menuOutput += "
    To Display part B of Random Integers enter 2 ";
    : menuOutput += "
    To display part C of Random Integers enter 3 ";
    : menuOutput += "
    To display part D of Random Integers enter 4 ";
    : menuOutput += "
    To terminate this program of Random Interger enter 0 ";
    : return menuOutput;
    : }
    :
    : public static int getNum(){
    : double theSeed = Math.random();
    : int newNum = (int)(Math.round(theSeed * 100) );
    : return newNum;
    : }
    :
    : // display of unsorted list
    : public static void displayList(IntNode head, IntNode curr){
    : String output = "";
    : for(curr = head; curr != null; curr = curr.getNext())
    : output += " " + curr.getItem();
    : JOptionPane.showMessageDialog(null, output, "Linked List Display",
    : JOptionPane.INFORMATION_MESSAGE);
    : }
    :
    : public static int getUserInput(String prompt){
    : String inputStr = JOptionPane.showInputDialog(prompt);
    : int input = new Integer(inputStr.trim()).intValue();
    : return input;
    : }
    :
    : } // end of class LLSort
    :
    :
    : [/code]
    :

  • mdw1982mdw1982 Member Posts: 124
    : Yeah, I did an error in my post - sorry :-(
    : This works now:
    :
    : [code]
    : while (count < numNode) {
    : newNode = new IntNode(getNum());
    : count++;
    :
    : //if the head is not initialized, then do it now
    : if ( head == null ) head = newNode;
    : //else add newNode to last one
    : else curr.setNext(newNode);
    :
    : //remember newNode as the last one
    : curr = newNode;
    : }
    : [/code]
    :
    : tron.
    :

    Ah, heavens! no problem... you're a treasure!

    Thanks a ton...now I can get on and get started on the list sort method.

    Mark
  • mdw1982mdw1982 Member Posts: 124
    man! this must be my day to be a bone head, but do you see any reason why this method should [b]not[/b] be inserting a new node into the list? the program compiles and runs fine, it just doesn't appear to be inserting any new values into the list.


    [code]
    public static void insertItems(IntNode head, IntNode curr, IntNode newNode){
    head = newNode;
    newNode = new IntNode(getUserInput("Enter an Integer Value"));
    while( curr.getNext() != null ){
    curr = curr.getNext();
    curr.setNext(newNode);
    }
    }
    [/code]

    the method [b]getUserInput()[/b] looks like this:

    [code]

    public static int getUserInput(String prompt){
    String inputStr = JOptionPane.showInputDialog(prompt);
    int input = new Integer(inputStr.trim()).intValue();
    return input;
    }

    [/code]
  • : man! this must be my day to be a bone head, but do you see any reason why this method should [b]not[/b] be inserting a new node into the list? the program compiles and runs fine, it just doesn't appear to be inserting any new values into the list.
    :
    :
    : [code]
    : public static void insertItems(IntNode head, IntNode curr, IntNode newNode){
    : head = newNode;
    : newNode = new IntNode(getUserInput("Enter an Integer Value"));
    : while( curr.getNext() != null ){
    : curr = curr.getNext();
    : curr.setNext(newNode);
    : }
    : }
    : [/code]
    :
    : the method [b]getUserInput()[/b] looks like this:
    :
    : [code]
    :
    : public static int getUserInput(String prompt){
    : String inputStr = JOptionPane.showInputDialog(prompt);
    : int input = new Integer(inputStr.trim()).intValue();
    : return input;
    : }
    :
    : [/code]
    :

    I do not understand why your method has 3 parameters, but see this code for an example. It inserts one list "listhead" into another list indexed by "indexnode":

    [code]
    //indexnode and listhead must not be null

    public void insertList(IntNode indexnode, IntNode listhead) {
    //remember the next element
    IntNode oldnext = indexnode.getNext();

    //insert list
    indexnode.setNext(listhead);

    //find the end of the inserted list
    IntNode theend = listhead;
    while (theend.getNext() != null) theend = theend.getNext();

    //repair the list
    theend.setNext(oldnext);
    }
    [/code]

    tron.
  • mdw1982mdw1982 Member Posts: 124
    : I do not understand why your method has 3 parameters, but see this code for an example. It inserts one list "listhead" into another list indexed by "indexnode":
    :
    : [code]
    : //indexnode and listhead must not be null
    :
    : public void insertList(IntNode indexnode, IntNode listhead) {
    : //remember the next element
    : IntNode oldnext = indexnode.getNext();
    :
    : //insert list
    : indexnode.setNext(listhead);
    :
    : //find the end of the inserted list
    : IntNode theend = listhead;
    : while (theend.getNext() != null) theend = theend.getNext();
    :
    : //repair the list
    : theend.setNext(oldnext);
    : }
    : [/code]
    :
    : tron.
    :

    well...I passed in head because as I understand it that is how you pass the list from one method to another; I passed in newNode to make the new node, and I passed in curr for the obvious reasons.

    I'm having a bit of a rough time wrapping my brain around references instead of solid, written-in-stone variables.

    Mark
Sign In or Register to comment.