NullPointerException when Creating an Array of objects [duplicate]
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have been trying to create an array of a class containing two values but when I try to apply a value to the array I get a NullPointerException.
public class ResultList {
public String name;
public Object value;
public ResultList() {}
}
.
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0].name = "iiii";
}
}
Why am I getting this error and how can I fix it?
java arrays nullpointerexception
marked as duplicate by Makoto
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have been trying to create an array of a class containing two values but when I try to apply a value to the array I get a NullPointerException.
public class ResultList {
public String name;
public Object value;
public ResultList() {}
}
.
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0].name = "iiii";
}
}
Why am I getting this error and how can I fix it?
java arrays nullpointerexception
marked as duplicate by Makoto
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
4
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have been trying to create an array of a class containing two values but when I try to apply a value to the array I get a NullPointerException.
public class ResultList {
public String name;
public Object value;
public ResultList() {}
}
.
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0].name = "iiii";
}
}
Why am I getting this error and how can I fix it?
java arrays nullpointerexception
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have been trying to create an array of a class containing two values but when I try to apply a value to the array I get a NullPointerException.
public class ResultList {
public String name;
public Object value;
public ResultList() {}
}
.
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0].name = "iiii";
}
}
Why am I getting this error and how can I fix it?
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
java arrays nullpointerexception
java arrays nullpointerexception
edited Dec 28 '12 at 9:23
user166390
asked Dec 17 '09 at 15:49
marjasinmarjasin
153124
153124
marked as duplicate by Makoto
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Makoto
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
4
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27
add a comment |
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
4
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
4
4
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27
add a comment |
9 Answers
9
active
oldest
votes
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
add a comment |
ResultList boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
add a comment |
i think by calling
ResultList boll = new ResultList[5];
you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.
boll[0] = new ResultList();
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
add a comment |
As many have said in the previous answers, ResultList boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
add a comment |
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
add a comment |
ResultList p = new ResultList[2];
By writing this you just allocate space for a array of 2 elements.
You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
add a comment |
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null.
So just assign the Object on that index and then set the value.
add a comment |
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String args) {
ResultList boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
add a comment |
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
add a comment |
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
add a comment |
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
answered Dec 17 '09 at 15:50
Nathan HughesNathan Hughes
73.1k16131211
73.1k16131211
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
add a comment |
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
5
5
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.
– enguerran
Dec 17 '09 at 15:53
add a comment |
ResultList boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
add a comment |
ResultList boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
add a comment |
ResultList boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
ResultList boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
answered Dec 17 '09 at 15:55
chburdchburd
3,6792332
3,6792332
add a comment |
add a comment |
i think by calling
ResultList boll = new ResultList[5];
you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.
boll[0] = new ResultList();
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
add a comment |
i think by calling
ResultList boll = new ResultList[5];
you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.
boll[0] = new ResultList();
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
add a comment |
i think by calling
ResultList boll = new ResultList[5];
you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.
boll[0] = new ResultList();
i think by calling
ResultList boll = new ResultList[5];
you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.
boll[0] = new ResultList();
answered Dec 17 '09 at 15:51
ufukgunufukgun
4,14062550
4,14062550
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
add a comment |
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
Mas 10 buen hombre ❤️❤️❤️
– Marinha do Nascimento
May 17 '18 at 14:27
add a comment |
As many have said in the previous answers, ResultList boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
add a comment |
As many have said in the previous answers, ResultList boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
add a comment |
As many have said in the previous answers, ResultList boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
As many have said in the previous answers, ResultList boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
edited Jan 22 '16 at 1:01
TylerH
15.7k105368
15.7k105368
answered Jan 7 '13 at 18:42
RishikeshDhokareRishikeshDhokare
2,3161427
2,3161427
add a comment |
add a comment |
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
add a comment |
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
add a comment |
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
answered Dec 4 '15 at 18:55
spazBarnumspazBarnum
462
462
add a comment |
add a comment |
ResultList p = new ResultList[2];
By writing this you just allocate space for a array of 2 elements.
You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
add a comment |
ResultList p = new ResultList[2];
By writing this you just allocate space for a array of 2 elements.
You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
add a comment |
ResultList p = new ResultList[2];
By writing this you just allocate space for a array of 2 elements.
You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
ResultList p = new ResultList[2];
By writing this you just allocate space for a array of 2 elements.
You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
edited Jul 17 '17 at 22:04
answered Jul 4 '17 at 6:48
Usama TahirUsama Tahir
295
295
add a comment |
add a comment |
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null.
So just assign the Object on that index and then set the value.
add a comment |
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null.
So just assign the Object on that index and then set the value.
add a comment |
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null.
So just assign the Object on that index and then set the value.
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null.
So just assign the Object on that index and then set the value.
answered Oct 28 '17 at 21:04
AnupAnup
113
113
add a comment |
add a comment |
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String args) {
ResultList boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
add a comment |
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String args) {
ResultList boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
add a comment |
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String args) {
ResultList boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String args) {
ResultList boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
edited Jan 1 '18 at 23:45
cricket_007
81.6k1143111
81.6k1143111
answered Jul 14 '17 at 14:05
arshadImamarshadImam
11
11
add a comment |
add a comment |
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
add a comment |
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
add a comment |
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String args){
ResultList boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
edited Jan 1 '18 at 23:45
cricket_007
81.6k1143111
81.6k1143111
answered Nov 22 '17 at 7:21
Hrishabkumar jhaHrishabkumar jha
93
93
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
add a comment |
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Either you can try the above scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
– Hrishabkumar jha
Nov 22 '17 at 7:23
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
Could you explain what you did to fix the problem?
– Qwertie
Nov 22 '17 at 7:39
1
1
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This was already suggested in several answers.
– shmosel
Nov 22 '17 at 7:40
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Mamun
Nov 22 '17 at 8:25
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
@Mamun It does though.
– shmosel
Nov 22 '17 at 22:43
add a comment |
It might be a little clearer to rename ResultList to something like NameValuePair.
– Nathan Hughes
Dec 17 '09 at 15:59
You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.
– enguerran
Dec 17 '09 at 16:18
4
A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.
– Peter Lawrey
Dec 17 '09 at 21:27