473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Can someone tell me were the problem is, Please?

thatos
105 100+
I am trying to write an program which performs a breath first search on a graph and returns a tree.
Expand|Select|Wrap|Line Numbers
  1. class Alist{
  2.     int x;
  3.     Alist next;
  4.     public Alist (){        
  5.     }
  6.     public Alist(int x){
  7.         this.x = x;
  8.         next = null;
  9.     }    
  10. }
  11.  
Expand|Select|Wrap|Line Numbers
  1. //Under Graph class
  2.  
  3. protected Alist list;
  4. int parent[];
  5.   public void removeFirst(){
  6.       list = list.next;
  7.   }
  8.   public int first(){
  9.       return list.x;
  10.   }
  11.   public void addLast(int x){
  12.       Alist curr = list;
  13.       Alist temp = new Alist(x);
  14.       while(curr.next != null){
  15.           curr = curr.next;
  16.       }
  17.       curr.next = temp;
  18.  
  19.   }
  20.   public boolean empty(){
  21.       if(list != null){
  22.           return false;
  23.       }
  24.       return true;
  25.   }
  26.   public void BreathFirst(int v){
  27.       int parent[] = new int[numvertices];
  28.       boolean marked[] = new boolean[numvertices];
  29.       for(int i=0;i<numvertices;i++){
  30.           marked[i] = false;
  31.       }
  32.       list = new Alist(v);
  33.       marked[v] = true;
  34.       int w;
  35.       while (!empty()){
  36.           w = first();
  37.           removeFirst();
  38.           for(int x=0;x<numvertices;x++){
  39.               if(isedge(w,x) && !marked[x]){
  40.                   marked[x] = true;
  41.                   parent[x] = w;
  42.                   addLast(x);
  43.               }
  44.           }
  45.       }
  46.       for(int i=0;i<numvertices;i++){
  47.           System.out.println(parent[i]+" "+i);
  48.       }
  49.  
  50.   }
  51.  
Here is the main method
Expand|Select|Wrap|Line Numbers
  1. Graph g;
  2. int n;
  3.  
  4.  
  5. SimpleInp inp = new SimpleInp("E:/graph.txt");
  6.  
  7. n = inp.readInt();
  8. g = new Graph(n, false);
  9.  
  10. g.read(inp);
  11. g.BreathFirst(0);//Everything works except this point
  12.  
The error which it returns
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.NullPointerException
  2.     at Graph.addLast(Graph.java:292)
  3.     at Graph.BreathFirst(Graph.java:320)
  4.     at Driver.main(Driver.java:19)
  5.  
If you need additional info just ask?
May 28 '08 #1
8 1425
BigDaddyLH
1,216 Expert 1GB
Your error is on line 292, right?

Do you know what a NullPointerException is? It is the most common programming error.

You are trying to dereference a null reference on that line.
May 28 '08 #2
thatos
105 100+
Your error is on line 292, right?

Do you know what a NullPointerException is? It is the most common programming error.

You are trying to dereference a null reference on that line.
Can you plsk tell me how can I resolve my problem,I know what NullPointerException is, but I do not know were is it there cause list references something.
May 28 '08 #3
BigDaddyLH
1,216 Expert 1GB
Can you plsk tell me how can I resolve my problem,I know what NullPointerException is, but I do not know were is it there cause list references something.
We're both in the dark. I don't know what line 292 is!-
May 28 '08 #4
thatos
105 100+
We're both in the dark. I don't know what line 292 is!-
Here is line 292
Expand|Select|Wrap|Line Numbers
  1. list = new Alist(v);
  2.  
May 28 '08 #5
thatos
105 100+
Here is line 292
Expand|Select|Wrap|Line Numbers
  1. list = new Alist(v);
  2.  
I made a mistake the line 292 is
Expand|Select|Wrap|Line Numbers
  1. addLast(x);
  2.  
May 28 '08 #6
BigDaddyLH
1,216 Expert 1GB
Here is line 292
Expand|Select|Wrap|Line Numbers
  1. list = new Alist(v);
  2.  
Are you sure it isn't:

Expand|Select|Wrap|Line Numbers
  1. while(curr.next != null){
...because there is nothing in the line you quote that can cause a NullPointerException!

Suggestion: clean and build. Delete all your *.class files and recompile. This won't fix your bug but maybe something is out of synch.
May 28 '08 #7
thatos
105 100+
Are you sure it isn't:

Expand|Select|Wrap|Line Numbers
  1. while(curr.next != null){
...because there is nothing in the line you quote that can cause a NullPointerException!

Suggestion: clean and build. Delete all your *.class files and recompile. This won't fix your bug but maybe something is out of synch.
You are right, I changed addLast to this
Expand|Select|Wrap|Line Numbers
  1. public void addLast(int x){
  2.       Alist curr = list;
  3.       Alist temp = new Alist(x);
  4.       while(curr != null){
  5.           curr = curr.next;
  6.       }
  7.       curr = temp;
  8.  
  9.   }
  10.  
May 28 '08 #8
BigDaddyLH
1,216 Expert 1GB
You are right, I changed addLast to this
Expand|Select|Wrap|Line Numbers
  1. public void addLast(int x){
  2.       Alist curr = list;
  3.       Alist temp = new Alist(x);
  4.       while(curr != null){
  5.           curr = curr.next;
  6.       }
  7.       curr = temp;
  8.  
  9.   }
  10.  
Woo-hoo! I thought it was because curr was null!
May 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

114
by: muldoon | last post by:
Americans consider having a "British accent" a sign of sophistication and high intelligence. Many companies hire salespersons from Britain to represent their products,etc. Question: When the...
5
by: Nafai | last post by:
Hello. I need to handle cin errors like these: .... int n; cout << "Type a number: "; cin >> n; // Check user actually typed a number. ....
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
18
by: Chris Botha | last post by:
Hi, I've tried the SQL Server newsgroups and it seems nobody did it there, I got no replies, or those that did it don't want to tell. I have VS2005 Beta 2 installed with VS2003 and everything...
5
by: jignasuk | last post by:
Hello, 1 unsigned char ar = "AB"; 2 unsigned int i = 0; 3 i = *(unsigned int *)ar; How does it convert to unsigned int from unsigned char()? After executing this three line I am getting...
5
by: garyusenet | last post by:
I understand that Point is an object used for storing co-ordinates, such as mouse location. I can't figure out what the e's do in the above example can someone explain please. Thanks, Gary.
3
by: sparks | last post by:
I have 2 fields that I have to make sure don't get screwed up. (could be one it doesn't matter) but what they want is no duplicates, warn the user changing existing numbers is a no no. (of course...
40
by: aslamhenry | last post by:
please key in any 5 digits number : 56789 and the ouput is 5678 9 567 89 56 789 5 6789
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.