-1

My eYMethods class has 2 static methods the first one is writeUSB that fills my object array with my scanner and I want to stop creating object arrays if (sumMemory > 80) and delete the object that passed the condition. But when I do that, my second static method showDocs that I call in my main class points null cause example: (if I create 2 object arrays and the first one override the condition of sumMemory, the second object array waits from my getters some values so it points null ). How can I fix that?

package eymain;

public class eYMethods {

    static int writeUSB(ekpaideytikoYliko usb[]) {

        double sumMemory = 0 ;
        int noOfObjects = 0;
        for(int i = 0; i < usb.length; i++) {                  
            System.out.println("Δωσε fileName : ");
            String fileName = scannerUserInput.getString();
            System.out.println("Δωσε minutes : ");
            double minutes = scannerUserInput.getDouble();
            System.out.println("Δωσε memorySpace");
            double memorySpace = scannerUserInput.getDouble();
            System.out.println();
            ekpaideytikoYliko tempEkpaideytikoYliko = new ekpaideytikoYliko(fileName, minutes, memorySpace);
            usb[i] = tempEkpaideytikoYliko;
            noOfObjects++;
            sumMemory += memorySpace;
            if (sumMemory > 80) {  
                noOfObjects--;                
                System.out.println("OverLimit");
                break;               
            }
        }

        System.out.println("sumMemory : " + sumMemory);
        return noOfObjects;

    }
    static void showDocs(ekpaideytikoYliko usb[]) {

        for(int i =0; i < usb.length; i++) {
            System.out.println("fileName : " + usb[i].getFileName());
            System.out.println("minutes : " + usb[i].getMinutes());
            System.out.println("memorySpace : " + usb[i].getMemorySpace());
            System.out.println();
        }
    }
 }
3
  • what's your ekpaideytikoYliko? Commented Aug 31, 2019 at 13:00
  • You probably need to post a valid minimal reproducible example Commented Aug 31, 2019 at 13:01
  • Its my object array that i have created in another class i have posted a similar question with all my code here link check it there so i don't duplicate the issue. Commented Aug 31, 2019 at 13:02

2 Answers 2

1

OK, so it looks like the problem is occurring because usb[i] is null and you trying to do this:

 System.out.println("fileName : " + usb[i].getFileName());

which will attempt to call a method on null and give you an NPE.

Solution #1: test for null.

static void showDocs(ekpaideytikoYliko usb[]) {
    for (int i = 0; i < usb.length && usb[i] != null; i++) {
         ....
    }
}

Solution #2: pass in and use noOfObjects

static void showDocs(ekpaideytikoYliko usb[], int noOfObjects) {
    for (int i = 0; i < usb.noOfObjects; i++) {
         ....
    }
}

Solution #3: use List<ekpaideytikoYliko> instead of ekpaideytikoYliko[]

This is cleanest because it solves another problem as well. If you use a List, you don't have to preallocate an array that is "large enough", and hope that it is.


Style.

According to Google translate, "ekpaideytikoYliko" is two words: probably εκπαιδευτικ Υλικο, transliterated into Roman characters. So the correct class name should be:

  EkpaideytikoYliko

A Class name should always start with an uppercase letter.

or some variation

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

11 Comments

Thanks very much I don't get the error it was so simple but couldn't think of it.
correct me if i'm wrong, but i don't think solution #2 will Not work since his ekpaideytikoYliko class doesn't expose a member (or a get method) for noOfObjects
@DanielK - I am suggesting he passes it as a second parameter. Look at the new method signature.
ohh yeah correct i didn't notice that,sorry.
Its Greek word and its for a homework I didn't create that variables I'm following the exercise.
|
1

If I understood you correctly, your trying to find a way to not add the ekpaideytikoYliko tempEkpaideytikoYliko = new ekpaideytikoYliko(fileName, minutes, memorySpace); to your usb array when the sumMemory become greater than 80, if that's the case than you can do it easily by addidng the tempEkpaideytikoYliko to the array after checking if sumMemory is less or equal to 80:

static int writeUSB(ekpaideytikoYliko usb[]) {

        double sumMemory = 0 ;
        int noOfObjects = 0;
        for(int i = 0; i < usb.length; i++) {                  
            System.out.println("Δωσε fileName : ");
            String fileName = scannerUserInput.getString();
            System.out.println("Δωσε minutes : ");
            double minutes = scannerUserInput.getDouble();
            System.out.println("Δωσε memorySpace");
            double memorySpace = scannerUserInput.getDouble();
            System.out.println();

            sumMemory += memorySpace;
            if (sumMemory > 80) {                 
                System.out.println("OverLimit");
                break;               
            }else{
                ekpaideytikoYliko tempEkpaideytikoYliko = new ekpaideytikoYliko(fileName, minutes, memorySpace);
                usb[i] = tempEkpaideytikoYliko;
                noOfObjects++;
            }
        }

        System.out.println("sumMemory : " + sumMemory);
        return noOfObjects;

    }

18 Comments

Yes i don't want to add it but still with your code i still create object arrays after the sumMemory passing the condition. You also forgot the } in the for loop to close it
I can't execute it I have an error with the continue tag.
weird, can you please tell me what's the error message?
Thanks mate appreciate it!
haha thanks i ll upvote you
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.