2

I have 3 classes one EyMain that is my main class where I read a "n" value > 100 that creates the number of my object arrays.Then I call writeUSB() method where I fill the object array.

public class EyMain {

    public static void main(String[] args) {

        int n;

        do {       
            System.out.println("Give an integer value > 100 : ");
            n = scannerUserInput.getInteger();
        } while (n < 101);

        ekpaideytikoYliko usb[] = new ekpaideytikoYliko[n];

        eYMethods.writeUSB(usb);
        eYMethods.showDocs(usb);

        } 
}

My other class is eYMethods where I have my 2 static methods writeUSB that I want to return the pointer of the last element that have stored in my array because I want to check if my memorySpace > 8gb I want to remove it from the array and updating the last element of the array and showDocs that I want to print only the elements from the object array that the user has typed and print only the file extension with .doc or .docx .

package eymain;

public class eYMethods {

static double writeUSB(ekpaideytikoYliko usb[]) {

    for(int i = 0; i < usb.length; i++) {      

        System.out.println("Give fileName : ");
        usb[i].setFileName(scannerUserInput.getString());
        System.out.println("Give minutes : ");
        usb[i].setMinutes(scannerUserInput.getDouble());
        System.out.println("Give memorySpace");
        usb[i].setMemorySpace(scannerUserInput.getDouble());
    }


    return 0;

}
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());
    }
}}

And last class is my ekapideytikoYliko that I have my private variables, get and set, my constructor and a String method getFileType that I want to take from the fileName the extension from it. Example(.doc, .docx, .mp4).

package eymain;

public class ekpaideytikoYliko {

private String fileName;
private double minutes;
private double memorySpace;

ekpaideytikoYliko(String fileName, double minutes, double memorySpace) {

    this.fileName = fileName;
    this.minutes = minutes;
    this.memorySpace = memorySpace;

}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public double getMinutes() {
    return minutes;
}

public void setMinutes(double minutes) {
    this.minutes = minutes;
}

public double getMemorySpace() {
    return memorySpace;
}

public void setMemorySpace(double memorySpace) {
    this.memorySpace = memorySpace;
}

String getfileType(ekpaideytikoYliko usb[]) {

    int name = fileName.lastIndexOf(".");
    if (name == -1) {
        return "";
    }
    return fileName.substring(name);
}}

And my scannerUserInput file :

package eymain;

import java.util.Scanner;

public class scannerUserInput {

    static int getInteger(){
    Scanner ob = new Scanner(System.in);
      try{
    int i = ob.nextInt();
        return i;
      }
      catch(Exception e){
    return -1;
      }
}

    static byte getByte(){
    Scanner ob = new Scanner(System.in);
      try{
    byte b = ob.nextByte();
        return b;
      }
      catch(Exception e){
    return -1;
      }
}
    static short getShort(){
    Scanner ob = new Scanner(System.in);
      try{
    short s = ob.nextShort();
        return s;
      }
      catch(Exception e){
    return -1;
      }
}
    static long getLongInteger(){
    Scanner ob = new Scanner(System.in);
      try{
    long l = ob.nextLong();
        return l;
      }
      catch(Exception e){
    return -1;
      }
}
    static float getFloat(){
    Scanner ob = new Scanner(System.in);
      try{
    float f = ob.nextFloat();
        return f;
      }
      catch(Exception e){
    return -1;
      }
}
    static double getDouble(){
    Scanner ob = new Scanner(System.in);
      try{
    double d = ob.nextDouble();
        return d;
      }
      catch(Exception e){
    return -1;
      }
}
    static String getString(){
    Scanner ob = new Scanner(System.in);
      try{
    String s = ob.nextLine();
        return s;
      }
      catch(Exception e){
    return "";
      }
}

    static char getChar(){
    Scanner ob = new Scanner(System.in);
      try{
    char ch = ob.next().charAt(0);
        return ch;
      }
      catch(Exception e){
    return ' ';
      }
}    

} 

When I type inside the writeUSB method the data from the scanner I get an error in my first type.

8
  • 2
    " i get an error in my first type." - Note: when asking about errors you should post them in the most complete form possible, i.e. if you have a stacktrace post that as well and if it mentions lines in code you've posted mark those lines so that we don't have to guess. Commented Aug 30, 2019 at 14:34
  • Please provide the error you are getting. I believe you need to flush the scanner with java.util.Scanner.nextLine() after your other calls. Commented Aug 30, 2019 at 14:35
  • if you used the scanner you will get an error cause its a file that i have inside my project that has all the types of variables . I suggest you to use the default scanner Commented Aug 30, 2019 at 14:36
  • Give an integer value > 100 : 105 Give fileName : file Exception in thread "main" java.lang.NullPointerException at eymain.eYMethods.writeUSB(eYMethods.java:10) at eymain.EyMain.main(EyMain.java:16) this is my error message Commented Aug 30, 2019 at 14:38
  • Seems like the issue is with scannerUserInput. I can't seem to find ScannerUserInput Methods/Class in the code you provided above. Commented Aug 30, 2019 at 14:48

1 Answer 1

1

In java when you create an array of an object in your case it is:

ekpaideytikoYliko usb[] = new ekpaideytikoYliko[n];

Java Simply does this

// Lets say n = 5 for easier demonstration
{null, null, null, null, null}

Source: Initial Values of Variables

For all reference types (§4.3), the default value is null.

Now when in eYMethods when you try do call a method it just return nullpointer because that element is null. To fix that you need to create an object and store that object in array. Something like this:

for (int i = 0; i < usb.length; i++) {
    System.out.println("Give fileName : ");
    String fileName = scannerUserInput.getString();
    System.out.println("Give minutes : ");
    double minutes = scannerUserInput.getDouble();
    System.out.println("Give memorySpace");
    double memorySpace = scannerUserInput.getDouble();
    ekpaideytikoYliko tempEkpaideytikoYliko = new ekpaideytikoYliko(fileName, minutes, memorySpace);
    usb[i] = tempEkpaideytikoYliko;
}

Now java will create an Object of Class ekpaideytikoYliko and store it in array.

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

13 Comments

Thanks this worked . I wanted the temp object array that you did its the same with usb after your make it equal usb[i] = tempEkpaideytikoYliko; , right? So the setters aren't necessary?
@LazosPap While it may not be necessary right now. They might come in handy if you want to expand on this code. So I suggest you keep them. Besides that you are using getters in showDocs.
One other thing that i want to achieve is to show the total memory of the usb so if the usb memory > 8 gb(8000mb i want to count it as mb) and its the last object that override the 8 gb i want this to remove from the array and there is a chance that the object array will not be filled full i want the writeUSB to return the pointer of the last element. How can i achieve that?
@LazosPap I would suggest that you keep track of memory and objects stored in array and if the memory goes above limit then you can simply skip over the rest of the objects. Though I do not understand "return the pointer of last element?". Java doesn't have pointers. Even it does what do you want to do with it?
Should i do that if condition inside the for that i fill the object array?
|

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.