3

I have an object which has a name, String [] there is also a another String array which just stores the name of the object (The first parameter) which is in main.

import java.util.*;
class Dice{
    public String [] side;
    public String name;

    public Dice (String n, String ... a){
        name = n;
        side = a;

    }

    //Setter and Getter name
    public String getName(){
        return name;
    }
    public void setName(String n){
        name = n;
    }
}

The objects parameters are set in the main class.

Dice easy = new Dice("Green:","Brain","Brain","Brain","Foot Print","Foot Print","Shotgun");

The string array just stores the name Easy:.

I am trying to compare the two arrays by passing them into a method in main.

//Removeing the 3 dice which were picked form the cup of the current player
public static Dice [] cup(Dice [] a , String [] b){
    Dice [] currentCup = new Dice[a.length];


    for (int i = 0; i < b.length; i++) {
        if (b[i] == a[i].getName()) {
            currentCup[i].setName("");
        }
    }

    return currentCup;
}

If the name of the object equals the name in the String array the objects name should equal and empty String(" ").

I am getting a error

Exception in thread "main" java.lang.NullPointerException

I understand that an ArrayList is much better to use here as I can just .remove(i, elem). But I do not know how to pass an ArrayList into a constructor.

Also this is just pure practice for myself using arrays.

The result should be that if the Dice [].getName() equals easy the name of that Dice object should be an empty String " ".

Liam
  • 568
  • 2
  • 15
  • 1
    This question is kinda confusing, but I'll see if I can help. A `NullPointerException` aka NPE) means you're trying to access members (methods or public properties) of an object reference that is null. What line is throwing the NPE? Try stepping through the code with a debugger to see what's null. Your problem has nothing to do with setting while comparing, or any such, you just don't have everything initialized. – Taylor Sep 12 '19 at 14:24
  • @Taylor's advice to find out where that NPE is coming from is absolutely what you should be doing. In addition, I see that, in your `cup` method, you're creating a new array `currentCup`, and then returning it _without modifying it_. Which means that it will just be an array of all nulls. So that's something to keep an eye on as you debug your code. – Jordan Sep 12 '19 at 14:27
  • Please read: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it+ – GhostCat Sep 12 '19 at 14:31
  • Then you got your string comparison wrong, too: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – GhostCat Sep 12 '19 at 14:31
  • And finally: focus on asking **one** question. It is really not a good start to talk about "some NPE takes place", to then ask about something different. One question per question please. And for questions around "code not working", please see [mcve], – GhostCat Sep 12 '19 at 14:32

3 Answers3

1

I understand that an ArrayList is much better to use here as I can just .remove(i, elem). But I do not know how to pass an ArrayList into a constructor.

public Dice (String n, List<String> sideList){
    name = n;
    side = sideList.toArray(new String[sideList.size()]);

}

Also you are not doing anything with the below array.

Dice [] currentCup = new Dice[a.length];

I would highly encourage you to dry run the code by yourself. It seems that your code is not doing what you really what to do. :)

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
1

In cup method the return value currentCup is never assigned, therefore it is just a "sequence" of null. You need to initialize its members singularly (e.g currentCup[i] = a[i] as first operation inside the for). The code tries to do an operation on a null which is often impossible (depending by the operation). In this case the code cannot getName of a null: this throws the exception at runtime.

Also the code will crash if b is longer than a.

Hope I helped.

Sterconium
  • 559
  • 4
  • 20
0

put it in your if in method:

if (b[i] == a[i].getName()) {
    a[i].setName("");
    currentCup[i] = a[i];
}