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>();
Related
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:
Variable used in lambda expression should be final or effectively final
(9 answers)
Closed 4 years ago.
This question is already asked. But today I found something odd. For the following code:-
public static List<EsbBucketInstanceDefinition> convertBucketDefinitionList(List<BucketInstanceDefinitionV1> bucketInstanceDefinitionV1List) {
List<EsbBucketInstanceDefinition> response = new ArrayList<>();
List<EsbBucketInstanceDefinition> finalResponse = new ArrayList<>();
bucketInstanceDefinitionV1List.stream().forEach(e -> {
EsbBucketInstanceDefinition esbBucketInstanceDefinition = new EsbBucketInstanceDefinition();
esbBucketInstanceDefinition.setInstanceType(e.getInstanceType());
esbBucketInstanceDefinition.setReportingGroup(e.getReportingGroup());
esbBucketInstanceDefinition.setSliceVolume(e.getSliceVolume());
esbBucketInstanceDefinition.setCounterName(e.getCounterName());
esbBucketInstanceDefinition.setSubscriberGroupId(e.getSubscriberGroupId());
// response.add(esbBucketInstanceDefinition); compiler error variable used in lambda should be final or effective final
finalResponse.add(esbBucketInstanceDefinition);
});
return finalResponse;
}
For this works fine. Looks like only variable name finalResponse is working. How and why? Is it valid to do?
References may only be made to (effectively) final variables from within a lambda.
The reference held by finalResponse in effectively final, because it never changes. Note that changing the reference means assigning a new value to it, eg
finalResponse = someOtherList;
Changing the state of the object referred to (eg adding items to the list referred to by finalResponse) is irrelevant to what the value held by the variable finalResponse, ie
finalResponse.add(something);
Does not change the variable finalResponse; it only changes the object to which finalResponse refers.
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
This question already has answers here:
final variable in methods in Java [duplicate]
(5 answers)
Closed 7 years ago.
I've used final keyword with class, method, fieldsbut this the first i'm seeing something like this
final Customer c=new Customer();
could anyone help me to get what is the use of this?
it is constant reference - you cannot change its value it can be assigned only once when being defined
due to Wikipedia
an example:
final String string = "initial value";
string += " some new content"; //here compiler will raise an error due to you cannot change final value
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;
}