Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

nullpointerexception

0 views
Skip to first unread message

Mike B

unread,
Nov 27, 2004, 5:22:06 PM11/27/04
to
I'm getting a nullpointerexception in this code. I can't figure out why? Any
ideas how I can investigate?

The exception is when I try to sort an array that I "think" is populated.

=> Arrays.sort(categorySalesArray);

and then I get:
PaneApplication says hello world
Exception in thread "main" java.lang.NullPointerException
at java.util.Arrays.mergeSort(Arrays.java:1156)
at java.util.Arrays.sort(Arrays.java:1080)
at Prob7Application.PaneApplication(Test7Driver.java:42)
at Test7Driver.main(Test7Driver.java:10)
Press any key to continue . . .

>sscce>

import java.text.*;
import java.util.Arrays;
import java.util.Vector;

public class Test7Driver
{
public static void main (String[] args)
{
Prob7Application appSession = new Prob7Application();
appSession.PaneApplication();
System.exit(0);
}
}

class Prob7Application
{
CategorySales[] categorySalesArray = new CategorySales[4];

// constructor
public void Prob7Application()
{
System.out.println("Prob7Application says hello world");

categorySalesArray[0].setCategoryName("XCondiments");
categorySalesArray[0].setCategoryNumber(1);

categorySalesArray[1].setCategoryName("AConfections");
categorySalesArray[1].setCategoryNumber(2);

categorySalesArray[2].setCategoryName("VGrains/Cereal");
categorySalesArray[2].setCategoryNumber(3);

categorySalesArray[3].setCategoryName("Produce");
categorySalesArray[3].setCategoryNumber(4);
System.out.println("Prob7Application says goodbye world");
}

//method header
public void PaneApplication ()
{
System.out.println("PaneApplication says hello world");
Arrays.sort(categorySalesArray);
for (int i=0; i < 4; i++)
{
System.out.println("CatName:" +
categorySalesArray[i].getCategoryName() +
" catNumber:>" +
categorySalesArray[i].getCategoryNumber() +
" CatCount:" +
categorySalesArray[1].getCategoryCount());
}


} //end main
} //end class Prob7PaneApp

class CategorySales implements Comparable
{
// Declare instance variables
private String strCategoryName;
private int intCategoryCount= 0;
private int intCategoryNumber;

public CategorySales()
{
} // end constructor

/** set Category name
*/
public void setCategoryName(String name)
{ strCategoryName = name;
}

/** get Category Name
*/
public String getCategoryName()
{ return strCategoryName;
}

/** set Category number
*/
public void setCategoryNumber(int number)
{ intCategoryNumber = number;
}

/** get Category number
*/
public int getCategoryNumber()
{ return intCategoryNumber;
}

/** add a number to the count of objects sold in this category
*/
public void addCategoryCount(int number)
{ intCategoryCount += number;
}

// Returns category sold counter
public int getCategoryCount()
{ return intCategoryCount;
}


// Implements Comparable Interface
public int compareTo(Object o)
{ CategorySales category2 = (CategorySales) o;
return (this.getCategoryName().compareTo(category2.getCategoryName()));
} //end method compareTo
} // end class CategorySales

</sscce>

Thanks
--
Mike B


Matt Humphrey

unread,
Nov 27, 2004, 5:37:28 PM11/27/04
to

"Mike B" <mrcics2000-...@nomail.yahoo.com> wrote in message
news:41a90...@news1.prserv.net...

> I'm getting a nullpointerexception in this code. I can't figure out why?
Any
> ideas how I can investigate?
>
> The exception is when I try to sort an array that I "think" is populated.
>
> => Arrays.sort(categorySalesArray);
>
> and then I get:
> PaneApplication says hello world
> Exception in thread "main" java.lang.NullPointerException
> at java.util.Arrays.mergeSort(Arrays.java:1156)
> at java.util.Arrays.sort(Arrays.java:1080)
> at Prob7Application.PaneApplication(Test7Driver.java:42)
> at Test7Driver.main(Test7Driver.java:10)
> Press any key to continue . . .

The problem isn't in the sorting... see below.


>
>
>
> >sscce>
>
> import java.text.*;
> import java.util.Arrays;
> import java.util.Vector;
>
> public class Test7Driver
> {
> public static void main (String[] args)
> {
> Prob7Application appSession = new Prob7Application();
> appSession.PaneApplication();
> System.exit(0);
> }
> }
>
> class Prob7Application
> {
> CategorySales[] categorySalesArray = new CategorySales[4];

This allocates an array with 4 slots and the value of each slot is null.
You must explicitly create the CategorySales objects for each entry.

>
> // constructor
> public void Prob7Application()
> {
> System.out.println("Prob7Application says hello world");
>

This is not a constructor because it has the "void" type even though the
name matches the class name. It is a method that is never called. Drop the
"void."

Here is where you would create the CategorySales items, as:
for (int i = 0; i < categorySalesArray.length; ++i) {
categorySalesArray [i] = new CategorySales ();
}

> categorySalesArray[0].setCategoryName("XCondiments");
> categorySalesArray[0].setCategoryNumber(1);
>
> categorySalesArray[1].setCategoryName("AConfections");
> categorySalesArray[1].setCategoryNumber(2);
>
> categorySalesArray[2].setCategoryName("VGrains/Cereal");
> categorySalesArray[2].setCategoryNumber(3);
>
> categorySalesArray[3].setCategoryName("Produce");
> categorySalesArray[3].setCategoryNumber(4);
> System.out.println("Prob7Application says goodbye world");
> }
>
> //method header
> public void PaneApplication ()
> {
> System.out.println("PaneApplication says hello world");
> Arrays.sort(categorySalesArray);
> for (int i=0; i < 4; i++)

Technique note: Don't use a literal--use the actual size of the array.

I have not checked any deeper for correctness. Let us know what you find
out.

Cheers,
Matt Humphrey ma...@ivizNOSPAM.com http://www.iviz.com/


Mike B

unread,
Nov 27, 2004, 5:45:25 PM11/27/04
to
Matt Humphrey <ma...@ivizNOSPAM.com> wrote:
>
> I have not checked any deeper for correctness. Let us know what you
> find out.
>

D'oh.. how many errors can I make in a small piece of code? Thanks Matt!

--
Mike B


Mike B

unread,
Nov 27, 2004, 5:49:20 PM11/27/04
to
Meant to tell you. It worked like a charm after I fixed my mistakes you
pointed out. Thanks again.

--
Mike B


Tony Morris

unread,
Nov 27, 2004, 6:14:34 PM11/27/04
to

Skip

unread,
Nov 28, 2004, 11:02:20 AM11/28/04
to
> Meant to tell you. It worked like a charm after I fixed my mistakes you
> pointed out. Thanks again.

System.out.println("CatName:" +


categorySalesArray[i].getCategoryName() +
" catNumber:>" +
categorySalesArray[i].getCategoryNumber() +
" CatCount:" +
categorySalesArray[1].getCategoryCount());

shouldn't that last 1 be i ?


Mike B

unread,
Nov 28, 2004, 12:26:19 PM11/28/04
to


LOL, yes, when I tested the real code I spotted that typo. Thanks for
pointing it out though!

--
Mike B


0 new messages