Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change one value for a Object while comparing it to another array?

Tags:

java

arrays

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 " ".

like image 424
Liam Avatar asked May 17 '26 01:05

Liam


2 Answers

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. :)

like image 172
Jude Niroshan Avatar answered May 19 '26 13:05

Jude Niroshan


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.

like image 40
Sterconium Avatar answered May 19 '26 15:05

Sterconium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!