0

I am writing a short Java program that is supposed to take in a list of 5 integers and assign each to one of 6 members. I want to maximize the number of members who are receiving a value between their min and max values.
Anyways, for some reason that is unknown to me, my private member class seems to be throwing a NullPointerException, but I am at a complete loss as to why. Any suggestions would be appreciated.

import java.util.*;
public class assign {
    private class member {
        private String name;
        private int min;
        private int max;
        public member(String name, int min, int max) {
            this.name = name;
            this.min = min;
            this.max = max;
        }
    }

    public member[] members = {
        new member("Phil", 1, 20),
        new member("Molly", 1, 20),
        new member("Connor", 21, 40),
        new member("Sam", 21, 40),
        new member("Dan", 41, 60),
        new member("Theresa", 41, 60)
    };

    public void main(String[] args) {
        System.out.println("test");
        String s = StdIn.readLine();
        String[] temp = s.split(" ");
        ArrayList<Integer> nums = new ArrayList<Integer>(5);
        for (int i = 0; i < 5; i++) {
            nums.add(Integer.parseInt(temp[i]));
        }
        ArrayList<member> mems = new ArrayList<member>(Arrays.asList(members));

        for (int n : nums) {
            for (member m : mems) {
                if (n > m.min && n < m.max) {
                    System.out.println(m.name + " : " + n);
                    nums.remove(n);
                    mems.remove(m);
                }
            }
        }
        for (int n : nums) {
            member m = mems.remove(0);
            System.out.println(m.name + " : " + n);
        }
    }
}

From OP's comment:

java.lang.NullPointerException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav‌​a:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2) 
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Sam G-H
  • 627
  • 6
  • 17
  • You should really go back and read the FAQ. If you need help with code you've written, you need to post under which conditions, what you expect to actually happen, any stack traces, etc. – Sotirios Delimanolis Nov 03 '13 at 03:24
  • 1
    You need to indicate which line throws the NPE. – Hovercraft Full Of Eels Nov 03 '13 at 03:31
  • java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) – Sam G-H Nov 03 '13 at 03:31
  • There's no obvious issue without our knowing which line throws the exception. A variable on that line is null. Show the complete exception message in your question, and try to find the line in your code that is indicated in the exception message, and comment it for us. – Hovercraft Full Of Eels Nov 03 '13 at 03:31
  • the short answer is I have no idea what line is throwing the exception. You can see the error message for yourself. – Sam G-H Nov 03 '13 at 03:33
  • Again, please post the entire message in the question itself, not part of the message in a comment. Also, and again, as per your previously deleted question, **your current main method should be static.** – Hovercraft Full Of Eels Nov 03 '13 at 03:34
  • Error messages are messages. You can read them once yourself, and enable line numbers in your editor. – clwhisk Nov 03 '13 at 03:35
  • Honestly, have you used a debugger to step through your code? Have you monitored your variables as you step through to see if they are being set to the values you expect? Have you tried setting breakpoints so you can examine the states of objects at key points? Why don't you just try doing that first. – scottb Nov 03 '13 at 03:35
  • @scottb: he can't use a debugger nor step through his code because it won't run. See my answer to see why. – Hovercraft Full Of Eels Nov 03 '13 at 03:38
  • This is a bug in DrJava. Your `main` should have been `static`. Duplicate of http://stackoverflow.com/questions/7911004/nullpointerexcetion-native-method-accessor-hashing-words-issue – Kevin Panko Nov 03 '13 at 05:28

2 Answers2

3

I'm betting that your error is that your current main method is not static:

// this should be a static method
public void main(String[] args) {
  // ...
}

Solution: make the main method static as it should be.

public static void main(String[] args) {
  // ...
}

Once you fix this, you'll see other errors, mainly that you're trying to access non-static things in a static way. To fix this, you'll want to give your assign class (please rename this Assign so that it starts with an upper case letter) public methods to call, and then have your main method create an Assign instance and call these methods.

For instance here:

ArrayList<member> mems = new ArrayList<member>(Arrays.asList(members));

You're trying to use the non-static members array in a static context. You will want to only handle it inside your Assign class, inside of its public methods. Either that or make members static.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

First big question like every one how is your code running without a static main ?

Your code has lot of flows, Kindly revisit the logic and try to run different test cases against your code (valid ones and then invalid ones). This is a good problem statement.

As an e.g

for (int n : nums) {
            for (member m : mems) {
                if (n > m.min && n < m.max) {
                    System.out.println(m.name + " : " + n);
                    nums.remove(n);
                    mems.remove(m);
                }
            }
        }

You have ConcurrentModificationException waiting to happen.

Are you sure you want to have this line : nums.remove(n);
IndexOutOfBoundsException.

jits
  • 1
  • 1