I think my rsort()
method should work right I always get an exception:
In Thread "main": java.lang.NullPointerExeception at IntQueue.get(IntQueue.java:47) at V5.main(V5.java:88)
Why am I getting this exception and how can I handle it?
IntQueue.java:
class IntQueue
{
public int get()
{
int res = fyrsti.tala; // <---- this is line 47 -----
n--;
if( fyrsti == sidasti )
fyrsti = sidasti = null;
else
fyrsti = fyrsti.naest;
return res;
}
static class Hlekkur
{
int tala;
Hlekkur naest;
}
Hlekkur fyrsti;
Hlekkur sidasti;
int n;
public IntQueue()
{
fyrsti = sidasti = null;
}
public int first()
{
return fyrsti.tala;
}
public void put( int i )
{
Hlekkur nyr = new Hlekkur();
n++;
nyr.tala = i;
if( sidasti==null )
fyrsti = sidasti = nyr;
else
{
sidasti.naest = nyr;
sidasti = nyr;
}
}
public int count()
{
return n;
}
}
V5.java:
public class V5
{
public static void main(String [开发者_如何学编程] args)
{
IntQueue q = new IntQueue();
Random rand = new Random();
for(int i = 0; i!= 10000; i++)
q.put(rand.nextInt(1000));
q = rsort(q);
int last = q.get(); // <---- this is line 88 -----
while(q.count() != 0)
{
int x = q.get();
if(x < last)
System.out.println("Wrong");
System.out.println("Right");
last = x;
}
}
public static IntQueue rsort(IntQueue q)
{
IntQueue [] r = new IntQueue[10];
for(int i = 0; i!=10; i++)
r[i] = new IntQueue();
IntQueue q2 = q;
int i = 0, v=1;
while(i != 3)
{
while(q2.count() != 0)
{
int x = q.get();
r[(x/v)%10].put(x);
}
for(int j = 0; j!=0; j++)
{
if(r[j].count() != 0)
q2.put(r[j].get());
else
j++;
}
v *= 10;
i++;
}
return q2;
}
}
[ed. note: reordered and compacted code to make relevant lines more visible]
The exceptiion happens on line 47 of IntQueue
. And as a NullPointerException
, it can be due to the following reasons:
Thrown when an application attempts to use null in a case where an object is required. These include:
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
My guess (without seeing the code of IntQueue
is that it's because you are trying to autounbox an Integer
to int
, but the Integer
is null
.
When you have exception in Java you have enough information in stack trace to localize what cause the problem.
Your stacktrace
In Thread "main": java.lang.NullPointerExeception
at IntQueue.get(IntQueue.java:47)
at V5.main(V5.java:88)
It means that in your main method in V5 class at 88 line you call method get (from IntQueue). This method get() in line 47 operate on null value and this cause NullPointerException
Often when you have exception the best way is to set breakpoint and check variables in debug mode. I recommend you setting breakpoint in 47 line in the IntQueue and then you will see what is wrong.
Well, it depends on what IntQueue looks like. Apparently you're calling the get
method from IntQueue
from line 88 of V5.java, and that method is failing at line 47 of IntQueue.java. That could be due to passing a bad argument to the method, or setting up the IntQueue
incorrectly, or anything like that. It's pretty hard to tell without seeing the source to IntQueue
and not knowing which is line 88 of V5.java. (The code you've posted doesn't have 88 lines.)
I suggest you look at line 47 of IntQueue.java and examine what it's trying to dereference, and line 88 of V5.java to see what's calling it. Then you should be able to work out where the bug is.
精彩评论