Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2004, 06:29 AM   #16 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
I missed that, but the problem still remains. What happens the first time you attempt to add a node? "startingnode" is initialized as null when you instantiate the class. "curr" is set to "startingnode". "prev" is set to "curr". Hence, "prev" is null.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 09-11-2004, 03:48 PM   #17 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by Belisarius

Any guesses as to what will go wrong here? You'll get a NullPointerException (wish I had the spoiler tags from SomethingAwful right now).
You're funny.
__________________
Valmont is offline   Reply With Quote
Old 09-11-2004, 03:54 PM   #18 (permalink)
Sunshine
Registered User
 
Join Date: Sep 2004
Posts: 9
Sunshine is on a distinguished road
(Very confusing)

public class StudentList{

private Student startingnode;

public studentList(){
startingnode=null; //startingnode is initialized as null
}

public void add(Student newNode){

Student curr;
Student prev;

curr=startingnode; //curr is set to startingnode
prev=null; //prev is set to null or curr?

while(curr!=null){
prev=curr; //prev is set to curr?or delete this code.
curr=curr.getNext();
}

prev.setNext(newNode);
newNode.setNext(curr);

}
Sunshine is offline   Reply With Quote
Old 09-11-2004, 06:59 PM   #19 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Here is the sequence of events.

1: startingnode = null;
3: curr = startingnode; // startingnode is still null, and therefore so is curr
3: curr = curr.next(); // NullPointerException is thrown here

Now, there is code inbetween these points, but nothing that impacts on this set of events. You cannot call a method on a null object (with exceptions that are too advanced for this topic and don't apply in any case).
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting