How to pass values to my object array from my static method

Lazos Pap :

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.

Goion :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148457&siteId=1