Results 1 to 4 of 4
  1. #1
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default help with with my null error

    Java Code:
    public class Partition3 
    {
    
    public static void main(String[] args) 
        {
        int number;
        Scanner in = new Scanner(System.in);
        System.out.println("Please insert the number to be partitioned");
        number = in.nextInt();
        System.out.println(number);
        generator a = new generator(number);
        
    }
    }
    class generator
    {
    	public generator(int anumber)
    	{
    		ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
    		Partition(anumber);
    	}
    public void print(ArrayList<Integer> x) {
        for (int j = 0; j < x.size(); j++)
        	System.out.println(x.get(j) + " " +  x.get(j + 1) + "\n" + " ");
            //System.out.println("%d%c", x[j], j + 1 == x.size() ? '\n' : ' ');
    }
    
    public boolean equal(ArrayList<Integer> a, ArrayList<Integer> b) {
        if (a.size() != b.size()) return false;
        for (int i = 0; i < a.size(); i++)
            if (a.get(i) != b.get(i)) return false;
        return true;
    }
    
    public void add(ArrayList<Integer> x) {
    	Collections.sort(x);
    	    for (int i = 0; i < ret.size(); i++)
            if (x.equals(ret.get(i))) return;
        ret.add(x); //fvsfv
    }
    
    public ArrayList<Integer> construct(ArrayList<Integer> a, ArrayList<Integer> b) {
        for (int i = 0; i < b.size(); i++)
            a.add(b.get(i));
        return a;
    }
    
    public ArrayList<Integer> make(int a, int b) {
        ArrayList<Integer> x = new ArrayList<Integer>();
        x.add(a);
        x.add(b);
        return x;
    }
    
    public ArrayList<Integer> subset(ArrayList<Integer> x, int first, int last) { //[first,last)
       ArrayList<Integer> m = new ArrayList<Integer>();
        for (int i = first; i < last; i++)
            m.add(x.get(i));
        return m;
    }
    
    public void partition(ArrayList<Integer> x) {   
        add(x);
        for (int i = 0; i < x.size(); i++)
            for (int j = 1; j < x.get(i); j++)
                partition(construct(make(x.get(i) - j, j), construct(subset(x, 0, i), subset(x, i + 1, x.size()))));
    }
    
    void Partition(int N) {
        ArrayList<Integer> x = new ArrayList<Integer>();
        x.add(N);
        partition(x);
    
        for (int i = 0; i < ret.size(); i++)
            print(ret.get(i));
    }
    private  ArrayList<ArrayList<Integer>> ret;
    }
    when i run the program, i get a
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
        at generator.add(Partition3.java:41)
        at generator.partition(Partition3.java:67)
        at generator.Partition(Partition3.java:76)
        at generator.<init>(Partition3.java:23)
        at Partition3.main(Partition3.java:14)
    can anybody help me with this error? thanks

    edit*
    the program is to parse a number entered by the user
    so if you wanted to parse 4, you should get:
    4
    3 1
    2 2
    2 1 1
    1 1 1 1
    but not necessarily in that order
    Last edited by zhangster; 03-20-2010 at 12:34 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The stack trace is telling you that the add() method is using a variable whose value is null in a way that is invalid for null values.

    Use System.out.println() or some other method, to figure out which variable is the culprit. Candidates are pretty much limited to x and ret. Once you have figured out which of these is null ask yourself "where did I initialise that variable?" ie where did you give it (or thought you gave it!) a nonnull value?

    And then figure out why that didn't happen.

  3. #3
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default solved

    i solved it..i initialized ret again in the constructor >.>

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Great, I'm glad you sorted it out.

    (Actually what you did was declare another variable in the constructor so that the instance variable ret never got initialised. But I see what you mean.)

Similar Threads

  1. Replies: 2
    Last Post: 03-19-2010, 06:30 PM
  2. Null point exception Error
    By morya123 in forum New To Java
    Replies: 10
    Last Post: 11-25-2009, 12:22 PM
  3. Null Error
    By scoleman123 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  4. Null value
    By Michael in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:25 AM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •