Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "main" java.lang.NullPointerException

807601Feb 5 2008 — edited Feb 5 2008
I have this problem for my java class

I am supposed to make a class that creates an array list of another class i created

When i got to find an objects that are equal it gives me a nullpointer exception error

This is the original class:

import java.util.ArrayList;
import java.util.Collections;

/**
A purse holds a collection of coins.
*/
public class Purse
{
/**
Constructs an empty purse.
*/
public Purse()
{
coins = new ArrayList<String>();
}
/**
Adds a coin to the purse.
@param coinName the coin to add
*/
public void addCoin(String coinName)
{
coins.add(new String(coinName));
}

/**
Returns a string describing the object.
@return a string in the format "Purse[coinName1,coinName2,...]"
*/
public String toString()
{
String s = "Purse[";
if(coins.size() > 0){
s += coins.get(0);
for(int i = 1; i < coins.size(); i++){
s += "," + coins.get(i);
}






}
return s + "]";
}

public boolean sameCoins(Purse other)


{
int c = 0;
Collections.sort(coins);
Collections.sort(other.coins);

for(int i = 0; i<coins.size(); i ++){

if(coins.get(i).equals(other.coins.get(i))){

c = c+1;


}
else
{

c = c + 0;

}
}

if(c < coins.size()){


return false;
}
else
{
return true;
}
}











private ArrayList<String> coins;











public Object checkPurse(String coinName) {
// TODO Auto-generated method stub
return null;
}

public int checkCoins() {
// TODO Auto-generated method stub
return 0;
}
}


This is the class that creates an arraylist of the objects in the first class:

import java.util.ArrayList;


public class PursePile {

private ArrayList<Purse> pile;

public PursePile()
{
pile = new ArrayList<Purse>();
}


public void add(Purse p)
{
pile.add(p);
}

public Purse findAPursesWith(String coinName)
{
int x = 0;

for (int i = 0; i < pile.size(); i++)
{
if (pile.get(i).checkPurse(coinName).equals(true))
{
x = i;
}
}
return pile.get(x);
}

public Purse findWithMostCoins()
{
int x = 0;
int size = 0;

for (int i = 0; i < pile.size(); i++)
{
if (pile.get(i).checkCoins() > size)
{
x = i;
}
}
return pile.get(x);
}
}


And this is the testing class i am using:

public class PileTester {

public static void main(String[] args)
{
PursePile p = new PursePile();

Purse purse1 = new Purse();
Purse purse2 = new Purse();
Purse purse3 = new Purse();
Purse purse4 = new Purse();

purse1.addCoin("Nickel");
purse1.addCoin("Penny");
purse2.addCoin("Quarter");
purse2.addCoin("Dime");
purse3.addCoin("Dime");
purse4.addCoin("Nicke");
purse4.addCoin("Quarter");
purse4.addCoin("Dime");
purse4.addCoin("Nickel");

p.add(purse1);
p.add(purse2);
p.add(purse3);
p.add(purse4);

System.out.println(p.findAPursesWith("Penny"));
System.out.println(p.findWithMostCoins());

}
}



if i comment out the lines giving me the null pointer exception it runs fine
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2008
Added on Feb 5 2008
8 comments
314 views