SputnicK SputnicK - 3 months ago 21
Java Question

How to pass an array and its values from one class to another in Java?

I need to pass an array from

Main.java
to
methodClass.java
to set integer values, then back to
Main.java
for use in the main program. The problem is that when I execute the program I receive a null pointer exception. I know I should be using pass-by-reference but am having trouble achieving an error-free result. Here is my code:

Main class:


import java.util.Scanner;

// Java RPG

public class Main {

static Scanner input = new Scanner(System.in);
static String classInput;
static Boolean optionsClass = true;
static int[] playerStat;


public static void main(String[] args) {

while(optionsClass) {
System.out.println("--- Welcome to Java RPG ---");
System.out.println("What class do you want? (Warrior, Mage, Steed)");
classInput = input.nextLine();

methodClass classChoice = new methodClass();

if(classInput.equals("Warrior")) {
System.out.println("| Swing Attack - attacks between 1 and 5 times doing 2 damage each.");
System.out.println("| Thrust - stabs the opponent doing 6 damage");
System.out.println("| Shield - increases defense by 10%");

classChoice.classConfirm();
classInput = input.nextLine();


if(classInput.equals("Yes")) {
classChoice.Warrior(playerStat);
System.out.println(playerStat);
}

optionsClass = false;
if(classInput.equals("No"))
optionsClass = true;

}
}
}
}


Method class:


public class methodClass {

private int[] playerStat;

public int[] Warrior(int[] playerStat) {
this.playerStat[0] = 0;
this.playerStat[1] = 1;
this.playerStat[2] = 2;

return this.playerStat;
}
public void classConfirm() {
System.out.println("Do you want this class?");
}

}
}

Answer

Java is always pass by value (cannot be pass by reference). But it passes the reference of the object as value. So, to make use of this, it would be better if you use an ArrayList instead of an int[]. Also, please use camel case for method names and pascal case for class names.

import java.util.ArrayList;
import java.util.Scanner;

// Java RPG

public class Main {

    static Scanner input = new Scanner(System.in);
    static String classInput;
    static Boolean optionsClass = true;
    static ArrayList<Integer> playerStat = new ArrayList<Integer>();

    public static void main(String[] args) {

        while (optionsClass) {
            System.out.println("--- Welcome to Java RPG ---");
            System.out.println("What class do you want? (Warrior, Mage, Steed)");
            classInput = input.nextLine();

            MethodClass classChoice = new MethodClass();

            if (classInput.equals("Warrior")) {
                System.out.println("| Swing Attack - attacks between 1 and 5 times doing 2 damage each.");
                System.out.println("| Thrust - stabs the opponent doing 6 damage");
                System.out.println("| Shield - increases defense by 10%");

                classChoice.classConfirm();
                classInput = input.nextLine();

                if (classInput.equals("Yes")) {

                    classChoice.warrior(playerStat);
                    System.out.println(playerStat);
                }

                optionsClass = false;
                if (classInput.equals("No"))
                    optionsClass = true;

            }
        }
    }
}

Method class would be as wfollows (your code also had an extra } bracket at the end):

import java.util.ArrayList;

public class MethodClass {

    public void warrior(ArrayList<Integer> playerStat) {
        if (playerStat == null)
            playerStat = new ArrayList<Integer>();
        playerStat.add(0);
        playerStat.add(1);
        playerStat.add(2);

    }

    public void classConfirm() {
        System.out.println("Do you want this class?");
    }

}
Comments