The Artima Developer Community
Sponsored Link

Java Answers Forum
small problem

5 replies on 1 page. Most recent reply: Apr 29, 2003 10:31 PM by Oliver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

small problem Posted: Apr 28, 2003 12:33 AM
Reply to this message Reply
Advertisement
I cannot figuare out what is wrong with this, I am getting null pointer errors pointing to my while loop but I don't know what the deal is. This is a doubly linked list, next and previous are set to null when a new link is created and all the accessor functions work fine, so...

public int insert(Link l)
	{
		
		Link temp=head;
 
		if(head==null)
		{
			head=l;
		}
	
		while(temp.getNext()!=null)
		{
			if(temp.getNext()==null)
			{
				temp.setNext(l);
				l.setPrev(temp);
 
			}
			else
			{
				temp=temp.getNext();	
			}
		}
		return 0;
	}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: small problem Posted: Apr 28, 2003 2:46 AM
Reply to this message Reply
I would like to see your
getNext()
method please...

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: small problem Posted: Apr 28, 2003 10:28 AM
Reply to this message Reply
here it is:

public Link getNext()
	{
		return next;
	}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: small problem Posted: Apr 28, 2003 8:59 PM
Reply to this message Reply
I think the problem might here in the getNext() method since if the next is null then you can return it and when you try to return it might give a NullPointerException. Why don't you check whether the next is Null before returning and then return a valid object...???

James Patterson

Posts: 16
Nickname: jmep
Registered: Mar, 2003

Re: small problem Posted: Apr 29, 2003 9:51 AM
Reply to this message Reply
Hey Jake, consider what happens when head is null... Your head variable gets assigned to the Link but temp is still null. Then when you hit the while loop your asking to call a method on null (temp).

Hope this helps,
James

Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: small problem Posted: Apr 29, 2003 10:31 PM
Reply to this message Reply

i agree with Senthoorkumaran Punniamoorthy, and you can change the statements

Link temp=head;

if(head==null)
{
head=l;
}

//////////////////////////////////////////////
//to
//////////////////////////////////////////////

if(head==null)
{
head=l;
}
Link temp=head;


what do think of it ? or you can supply all your codes for our reference.

Flat View: This topic has 5 replies on 1 page
Topic: VB to VJ++ transition Previous Topic   Next Topic Topic: HELP: read in a line and stores it in another file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use