This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm making something but I can't seem to make it work. I get a nullpointerexception, but I can't figure out why.
public class Bus implements Serializable {
ArrayList<Reiziger> reizigers;
public String add(Reiziger reiziger) {
reizigers.add(reiziger);
return "lijst";
}
}
The nullpointer happens at line "reizigers.add(reiziger);" in the add method of Bus.java
You should initialize ArrayList<Reiziger> reizigers before use it:
List<Reiziger> reizigers = new ArrayList<>();
Also, note that List interface type is used for reizigers collections instead of ArrayList implementation. Read here the reason
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I'm trying something. I'm want to create new object and add in list. But I get NullPointerException. My code is
private List<HisseModel> hisseListe=null;`
HisseModel model = new HisseModel();
model.setSymbol("abv");
model.setChange("123");
model.setCurrency("234");
HisseModel model2 = new HisseModel();
model2.setSymbol("abv2");
model2.setChange("1232");
model2.setCurrency("2342");
hisseListe.add(model);
hisseListe.add(model2);`
Why it's not working ?
The problem is here which is assigned to null
private List<HisseModel> hisseListe=null;
You need to initialize it like below
private List<HisseModel> hisseListe=new ArrayList<>();
Problem is at below line
private List<HisseModel> hisseListe=null;
null is not an object, so you can't access it - that's a NullPointerException.
You'll need to initialize a list (like below) and then add object into it.
private List<HisseModel> hisseListe = new ArrayList <HisseModel>();
This question already has answers here:
Remove object from ArrayList with some Object property
(5 answers)
Remove objects from an ArrayList based on a given criteria
(10 answers)
How to remove specific object from ArrayList in Java?
(17 answers)
Closed 1 year ago.
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following code
HashSet<Data> set = new HashSet<>();
set.add(new Data(10, 5));
...
class Data {
public int importantVal;
public int notImportantVal;
//... constructor ...
#Override
public int hashCode() {
return importantVal;
}
}
and I knew the importantVal of a Data object, but not the object itself. How would I remove it? set.remove(10) does not work.
Best solution I can think of is to also override equals() to return if importantVal is the same, and then do set.remove(new Data(10, anyPlaceholderValue))
This question already has answers here:
Default constructor vs. inline field initialization
(5 answers)
Closed 3 years ago.
I have a class like:
public class TemplateFileResponse {
private String path;
private List<FileView> children;
}
I want to create an instance and set children is empty array. so what is the best way to do it?
You can create an empty list with the new operator:
public class TemplateFileResponse {
private String path;
private List<FileView> children = new ArrayList<>();
}
You may also want to initialize the path field, either in a constructor or inline, because otherwise it will be initialized to null by default.
I suggest that you read a tutorial about Java classes, constructors, methods, and instantiating objects to understand how all of this works.
This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
Closed 8 years ago.
I'm trying to write a program to create a media object, I created the Cassette class which inherits from Audio which inherits from Media. I'm getting a null pointer exception and I have been trying for hours to fix it but I have no idea why it's being thrown. I appreciate your help in advance, thank you.
In my application class I initiate the following:
static Media[] collection = new Media[100];
And later on in the code I try to create a new Cassette object but it gives me the said null pointer exception.
The code I have is:
collection[collection[0].getNumItems()] = new Cassette(cTitle, cMajorArtist, cPlayingTime, cNumPlays, cNumGroupMembers, cGroupMembers, pArtist, cNumSongs, cSongs);
All of the items being passed into the Cassette are all user input data. It compiles fine but it's just when I run it that I get an error.
EDIT
Here is the numItems value in my media class.
static int numItems = 0;
And the method to return the number of items:
public int getNumItems()
{
return numItems;
}
Thanks.
I suggest you use a Collection like ArrayList. With the diamond operator that would look something like,
static List<Media> collection = new ArrayList<>();
Then you can use List#add(E)
collection.add(new Cassette(cTitle, cMajorArtist, cPlayingTime,
cNumPlays, cNumGroupMembers, cGroupMembers, pArtist, cNumSongs, cSongs));
Edit If you want to use the array type, then this
collection[collection[0].getNumItems()]
Should be
collection[Media.getNumItems()]
To call the static getNumItems() in Media. Which should be
public static int getNumItems()
{
return numItems;
}
This question already has answers here:
Implementing toString on Java enums
(4 answers)
Closed 8 years ago.
I'm new to Java and I'm learning the language fundamentals.
Can someone explain to me how the toString method is called when there is no function call to it? I think it has something to do with the actual enumerator words on the second line such as:
KALAMATA("Kalamata"), LIGURIO("Ligurio") ...
The whole purpose for this enum class is so the ENUM values don't print to screen in all upper case characters.
Can someone please explain me how toString method is used in this class? Like when is it called? How is it called?
public enum OliveName {
KALAMATA("Kalamata"),LIGURIO("Ligurio"),PICHOLINE("Picholine"),GOLDEN("Golden");
private String nameAsString;
//for enum classes, the constructor must be private
private OliveName(String nameAsString) {
this.nameAsString = nameAsString;
}
#Override
public String toString() {
return this.nameAsString;
}
}
Pretty much like any object.
OliveName oliveName = OliveName.KALAMATA;
System.out.println(oliveName.toString());
or
System.out.println(oliveName);