AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

nullpointerexception

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Mike B
Guest





PostPosted: Sat Nov 27, 2004 10:22 pm    Post subject: nullpointerexception Reply with quote



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 . . .



Quote:
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



Back to top
Matt Humphrey
Guest





PostPosted: Sat Nov 27, 2004 10:37 pm    Post subject: Re: nullpointerexception Reply with quote




"Mike B" <mrcics2000-news-nomail (AT) nomail (DOT) yahoo.com> wrote

Quote:
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.


Quote:



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.

Quote:

// 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 ();
}

Quote:
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.

Quote:
{
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

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

Cheers,
Matt Humphrey [email]matth (AT) ivizNOSPAM (DOT) com[/email] http://www.iviz.com/



Back to top
Mike B
Guest





PostPosted: Sat Nov 27, 2004 10:45 pm    Post subject: Re: nullpointerexception Reply with quote



Matt Humphrey <matth (AT) ivizNOSPAM (DOT) com> wrote:
Quote:

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



Back to top
Mike B
Guest





PostPosted: Sat Nov 27, 2004 10:49 pm    Post subject: Re: nullpointerexception - PS Reply with quote

Meant to tell you. It worked like a charm after I fixed my mistakes you
pointed out. Thanks again.

--
Mike B


Back to top
Skip
Guest





PostPosted: Sun Nov 28, 2004 4:02 pm    Post subject: Re: nullpointerexception - PS Reply with quote

Quote:
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 ?



Back to top
Mike B
Guest





PostPosted: Sun Nov 28, 2004 5:26 pm    Post subject: Re: nullpointerexception - PS Reply with quote

Skip <a@b.invalid> wrote:
Quote:
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 ?


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

--
Mike B



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.