Hi,
i get an NullPointerException in this line:if (let==currentLink.aLetter) in search method.

This is node class

public class Letter {
    char aLetter;
    int points;
    Letter next;

    public Letter(char aLetter,int points)
    {
        this.aLetter=aLetter;
        this.points=points;
    }
       public Letter()
       {

       }
     public String printLink() {
          //  System.out.print("{" + aLetter + ", " + points + "} ");

         return aLetter+" "+points;
    }

}

This is the other class

public class CardClass {
     private Letter head;
   //  private Letter prev;
     private static int size=0;

     public CardClass()
     {
       head=new Letter();

          //prev=new Letter();
     }
 //Returns true if list is empty
    public boolean isEmpty() {
        if (head == null)
            return true;
        else
            return false;
    }
    public void insert(char aLetter, int points) {

            Letter link = new Letter(aLetter,points);


         if (size==0)
            {
               link.next=null;
               head.next=link;
            }
           if (size>0)
           {  
               link.next=head.next;
               head.next=link;
           }
            size++;
    }
    public int getSize()
    {
        return size;
    }
    public void search(char let)
    {       
         Letter currentLink = head.next;


         int i=0;
            while(i<size) {
                if (let==currentLink.aLetter)
                {

                   String str= currentLink.printLink();
                   Start ss=new Start();

                javax.swing.JTextField jj=   ss.getJTextField6();
                jj.setText(str);
                    break;
                }
                    currentLink = currentLink.next;
                    i++;
            }

    }
}

I dont unerstand why.
PLZ someone help me out.

cheers

Recommended Answers

All 7 Replies

NPE means one of the variables on that line is null (unitialised) when you try to use it. let and aLetter could legally be null in that statement, so the culprit must be currentLink. Put print statements into your code to track the value(s) of currentLink so you can see where its going wrong.

currentLink is pointing to head.next, which may well be null, hence the null pointer exception. Test it first using some code like this

if (null == currentLink)
{
    // have null pointer
}
else
{
    // Search or manipulate currentLink
}

i did it.
aLetter is null not let.
i grab the input correctly from the user so when it comes to this
Letter link = new Letter(aLetter,points); line. aLetter and points are not null.

So in the Letter constructor im initiating aLetter and Points.
Then when im retriving why it is null?
I think insert(..) method works fine.

What is the value of aLetter for the last node in the list? Is that null?

No it is not null.
As it is the last node LastNode.next=null.
If there is a way to attach my project folder i can attach it.

Trying to double-guess the bug(s) in your code is never easy. Did you put in print statements as suggested earlier? Inside your search loop print all the values of all the variables on each pass of the loop. Post the output here if it doesn't immediately point you to the cause of the problem.

i did it.
aLetter is null not let.

This isn't the problem you first posted... if (let==currentLink.aLetter) will not throw an NPE is aLetter is null, you can only get an NPE from currentLink being null in that statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.