0

This is the error i received when i tried running my code below. my teacher said we could ask anywhere for help. I would be very grateful if someone could help me with this error

java.lang.NullPointerException
    at Practise_Assessment.main(Practise_Assessment.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

here is my code if it is needed

import java.text.NumberFormat;
import java.util.Arrays;

public class Practise_Assessment {

    public static String absences(String prompt) {// user input for name and
    // days absent
        System.out.println(prompt);
        java.util.Scanner keyboard = new java.util.Scanner(System.in);
        return keyboard.nextLine();
    }


    public static void main(String[] args) throws Exception {

        int numDayEntries ;
        int numNameEntries;
        int[] daysArray = null ;
        String[] nameArray = null ;

        boolean done = false;

        while (!done) {
            System.out.println("enter # to stop");
            String absences = absences("enter name of person absent then the amount of days they were absent");// the value of the users input

            for(numDayEntries =0; numDayEntries < daysArray.length;numDayEntries++ ){
                daysArray = new int[numDayEntries];
            }
            for(numNameEntries = 0; numNameEntries < nameArray.length;numNameEntries++ ){

                nameArray = new String[numNameEntries];    
            }

            if (absences.equalsIgnoreCase("#")) {
                done = true;    
            } else {
                String DaysAbsent =absences.replaceAll("[^0-9]", "") ;
                int daysAbsent = ((Number) NumberFormat.getInstance().parse(DaysAbsent)).intValue();//   converts a string to an int
                String absentee = absences.replaceAll("\\d", "");// replace all digits in the user input to nothing leaving just the name

                daysArray[numDayEntries] = daysAbsent; 
                nameArray[numNameEntries] = absentee;
            }
        } 
        System.out.println(Arrays.toString(daysArray));
    }
}
3
  • 1
    Daysarray variable is null Commented Apr 1, 2014 at 5:35
  • thanks everyone who replied, you guys(and girls) helped so much Commented Apr 1, 2014 at 7:17
  • @user3023714 then, you should accept the answer which helped you to solve it. Commented Apr 1, 2014 at 7:39

6 Answers 6

2

Your for loop is counting to daysArray.length, but daysArray is null. Furthermore, your for loops don't make any sense anyway; you're creating and then throwing away lots of identical arrays. It's very unclear what your code is trying to accomplish, since you're asking for a single person's name and then a single number indicating the number of absences.

The recommended approach is to (1) use an object that contains both the student's name and the number of absences, keeping this information together in a single record and (2) use a List instead of an array if you don't know how many items you're going to need, since a List is of variable size.

Sign up to request clarification or add additional context in comments.

Comments

1

daysArray is null and .length is accessed from null reference. That's the cause of NPE.You need to initialize array before it is being used. Your for loop is not making any sense at all.

Comments

1

The stack trace is telling you that the problem is on line 35 of the Practise_Assessment class. You need to look at that line.

If line 35 is this:

for(numDayEntries =0; numDayEntries < daysArray.length;numDayEntries++ ){

then it is probably because you did not initialise daysArray - you set it to null.

Comments

1

daysArray is null numNameEntries IS NULL

Comments

1

Issues in your for loop. Your arrays are set to NULL.

  1. Initialize your array before going to use.

    Then try it , you'll get what you want.

Comments

1

This line is the cause of the exception - for(numDayEntries =0; numDayEntries < daysArray.length;numDayEntries++ ).

Look at the exception you got - NULL-PointerException or NPE. It tells us that some REFERENCE or TYPE has been set as NULL, that is, we did not assign an appropriate object to it. Eg. Car nfs = null;. We should have done Car nfs = new Car();

Now, the only OBJECT in the error causing line is daysArray. Everything else is an int. So, you should see the rest of your code if daysArray is being assigned an object properly or not. Maybe you forgot or maybe the it happens due to a logical error.

Long winded way, but I did it since you are new.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.