nullpointerexception

From: Mike B (mrcics2000-news-nomail_at_nomail.yahoo.com)
Date: 11/27/04


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 


Relevant Pages

  • Re: Java Hang Man game assistance
    ... GIF file into seperate elements and displaying those elements as the ... public int get_column{ ... public void paint{ ... public void mousePressed(MouseEvent e) ...
    (comp.lang.java.programmer)
  • Re: Adding to custom collection property in designer
    ... foreach (MyListBoxColumn ch in values) ... public void Insert ... public int IndexOf ... public bool IsSynchronized ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Hosting Preview Handler In DotNet Application?
    ... public int left; ... void QueryFocus(out IntPtr phwnd); ... public void PreView ... StreamWrapper stream = new StreamWrapper(File.Open(file, ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Stupid Calendar Class
    ... I have extended Calendar and now it is complaining (depending on which ... populated with MyCalendar ... public void setShortDateFormat() { ... public int getMinimum{ ...
    (comp.lang.java.programmer)
  • Stupid Calendar Class
    ... I have extended Calendar and now it is complaining (depending on which ... populated with MyCalendar ... public void setShortDateFormat() { ... public int getMinimum{ ...
    (comp.lang.java.programmer)