Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

NullPointerException error

Hi guys,
having a small error that I can't seem to fix for the life of me. Been googling around and I understand the error, but not sure how to go about fixing it so to speak.
Any help with my code is greatly appreciated.

Here is the error code:

Exception in thread "main" java.lang.NullPointerException
at enqueue.enqueueMethod(enqueue.java:22)
at enqueue.main(enqueue.java:8)

And here is my code:
Code
public class pizza
{
public String ingrediants;
public String address;
public pizza next;
public pizza(String ingrediants, String address)
{
this.address = address;
this.ingrediants = ingrediants;
next = null;
}
}

Code
public class enqueue {
public static void main(String[] args)
{
pizza first[] = null;
pizza last[] = null;

enqueueMethod(first, last, new pizza("pepperoni", "1234 Bobcat Trail"));
enqueueMethod(first, last, new pizza("sausage", "2345 University Drive"));
deliverMethod(first, last);
enqueueMethod(first, last, new pizza("extra cheese", "3456 Rickster Road"));
enqueueMethod(first, last, new pizza("everything", "4567 King Court"));
enqueueMethod(first, last, new pizza("coffee beans", "5678 Java Circle"));
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
}

public static void enqueueMethod(pizza[] head, pizza[] tail, pizza thispizza)
{
if (head[0] == null)
{
head[0] = thispizza;
}
else
{
tail[0].next = thispizza;
}
tail[0] = thispizza;
return;
}

public static pizza dequeueMethod(pizza[] head, pizza[] tail)
{
pizza pizzatodeliver = null;
if (head[0] != null)
{
pizzatodeliver = head[0];
head[0] = head[0].next;
}
if (head[0] == null)
{
tail[0] = null;
}
return pizzatodeliver;
}

public static void deliverMethod(pizza[] head, pizza[] tail)
{
pizza thispizza = dequeueMethod(head, tail);
if (thispizza == null)
{
System.out.println("No deliveries pending");
return;
}
System.out.println("Deliver a pizza with ");
System.out.println(thispizza.ingrediants);
System.out.println(" to ");
System.out.println(thispizza.address);
}
}
The problem here has to do with the "first" and "last" arrays. As you can see, you have set both to null at the start of your main method.

After you have set both arrays to null, you proceed by calling the "enqueueMethod" method. The first thing the enqueueMethod does is check if the first element of the "head" variable (which as you know, is the "first" array) is null. However, this won't work because the entire array is null. So Java tries to access the array to get the first element, but the array itself is null, so it won't work. Keep in mind that in Java, arrays are actually objects.

You can fix this pretty easily by doing the following:

Code
// instead of doing pizza first[] = null, do this:
pizza[] first = new pizza[number of elements you want in the array here];
pizza[] last = new pizza[same here];

See if that works, mate.
(Feb 11, 2018 10:08 PM)CAFEEFAC Wrote:  The problem here has to do with the "first" and "last" arrays. As you can see, you have set both to null at the start of your main method.

After you have set both arrays to null, you proceed by calling the "enqueueMethod" method. The first thing the enqueueMethod does is check if the first element of the "head" variable (which as you know, is the "first" array) is null. However, this won't work because the entire array is null. So Java tries to access the array to get the first element, but the array itself is null, so it won't work. Keep in mind that in Java, arrays are actually objects.

You can fix this pretty easily by doing the following:

Code
// instead of doing pizza first[] = null, do this:
pizza[] first = new pizza[number of elements you want in the array here];
pizza[] last = new pizza[same here];

See if that works, mate.

I freaking love ya m8. Thank you and it works now!
In the mean time before you replied, I completely re-wrote the program and somehow fixed it, but an entirely different way. Instead I did this:

Code
public class enqueue {
public static void main(String[] args) {
pizza first = null;
pizza last = null;

enqueueMethod(first, last, new pizza("pepperoni", "1234 Bobcat Trail"));
enqueueMethod(first, last, new pizza("sausage", "2345 University Drive"));
deliverMethod(first, last);
enqueueMethod(first, last, new pizza("extra cheese", "3456 Rickster Road"));
enqueueMethod(first, last, new pizza("everything", "4567 King Court"));
enqueueMethod(first, last, new pizza("coffee beans", "5678 Java Circle"));
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
deliverMethod(first, last);
return;
}

public static void enqueueMethod(pizza head, pizza tail, pizza thispizza) {
if (head == null) {
head = thispizza;
}
else {
tail.next = thispizza;
}
tail = thispizza;
return;
}

public static pizza dequeueMethod(pizza head, pizza tail) {
pizza pizzatodeliver = null;
if (head != null) {
pizzatodeliver = head;
head = head.next;
}
if (head == null) {
tail = null;
}
return pizzatodeliver;
}

public static void deliverMethod(pizza head, pizza tail) {
pizza thispizza = dequeueMethod(head,tail);
if (thispizza == null) {
System.out.println("No deliveries pending");
return;
}
System.out.print("Deliver a pizza with ");
System.out.print(thispizza.ingrediants);
System.out.print(" to ");
System.out.println(thispizza.address);
}
}

Although after some testing, the edit you made is more efficient and bug-less. So going to use it. =-)
Thanks again,


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by MohinderJan 10, 2017 01:30 PM 7 208 Jan 10, 2017 01:30 PM
Last Post: 3cli‌‌pse
[Java] by SunnyAkumaJul 5, 2015 05:15 PM 6 240 Jul 5, 2015 05:15 PM
Last Post: JavaProfit
[Java] by YellowMilkAug 30, 2014 02:23 AM 2 149 Aug 30, 2014 02:23 AM
Last Post: Cosa Nostra