nullpointerexception
From: Mike B (mrcics2000-news-nomail_at_nomail.yahoo.com)
Date: 11/27/04
- Next message: Matt Humphrey: "Re: nullpointerexception"
- Previous message: JS: "Re: Can't change the colors in my Frame"
- Next in thread: Matt Humphrey: "Re: nullpointerexception"
- Reply: Matt Humphrey: "Re: nullpointerexception"
- Reply: Tony Morris: "Re: nullpointerexception"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 16:22:06 -0600
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
- Next message: Matt Humphrey: "Re: nullpointerexception"
- Previous message: JS: "Re: Can't change the colors in my Frame"
- Next in thread: Matt Humphrey: "Re: nullpointerexception"
- Reply: Matt Humphrey: "Re: nullpointerexception"
- Reply: Tony Morris: "Re: nullpointerexception"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|