Why null pointer exception? [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
Why am I getting null pointer while running this program , at line 13 : dragons[i].strength = Integer.parseInt(input[0]);
??
public final class practice {
public static void main(String args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine().split(" ");
int n = Integer.parseInt(input[1]);
int s = Integer.parseInt(input[0]);
class dragon{
int strength; int points;
};
dragon dragons = new dragon[n];
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Arrays.sort(dragons, new Comparator<dragon>() {
@Override
public int compare(dragon o1, dragon o2) {
if(o1.strength < o2.strength)
return -1;
else if(o1.strength == o2.strength) {
if(o1.points < o2.points)
return -1;
return 1;
}
return 1;
}
});
for(int i = 0 ; i < n ; i = i + 1) {
System.out.print(dragons[i].strength + " " + dragons[i].points);
}
}
}
Sample input :
2 2
99 1
100 1
java nullpointerexception
marked as duplicate by Joe C, Wai Ha Lee, Pshemo
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();
}
);
});
});
Nov 22 at 8:14
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 |
up vote
-1
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
Why am I getting null pointer while running this program , at line 13 : dragons[i].strength = Integer.parseInt(input[0]);
??
public final class practice {
public static void main(String args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine().split(" ");
int n = Integer.parseInt(input[1]);
int s = Integer.parseInt(input[0]);
class dragon{
int strength; int points;
};
dragon dragons = new dragon[n];
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Arrays.sort(dragons, new Comparator<dragon>() {
@Override
public int compare(dragon o1, dragon o2) {
if(o1.strength < o2.strength)
return -1;
else if(o1.strength == o2.strength) {
if(o1.points < o2.points)
return -1;
return 1;
}
return 1;
}
});
for(int i = 0 ; i < n ; i = i + 1) {
System.out.print(dragons[i].strength + " " + dragons[i].points);
}
}
}
Sample input :
2 2
99 1
100 1
java nullpointerexception
marked as duplicate by Joe C, Wai Ha Lee, Pshemo
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();
}
);
});
});
Nov 22 at 8:14
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.
2
When you dodragon dragons = new dragon[n]
your array only containsnull
values, you have to populate it with actualdragon
objects. Also by convention, class names should start with an upper-case letter, i.eDragon
.
– Arnaud
Nov 22 at 8:04
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to dodragon dragons = new dragon();
before setting its attributes.
– cactuschibre
Nov 22 at 8:05
1
Create a new dragon, eg.dragons[i] = new dragon();
otherwise the array is full of null.
– matt
Nov 22 at 8:10
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
Why am I getting null pointer while running this program , at line 13 : dragons[i].strength = Integer.parseInt(input[0]);
??
public final class practice {
public static void main(String args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine().split(" ");
int n = Integer.parseInt(input[1]);
int s = Integer.parseInt(input[0]);
class dragon{
int strength; int points;
};
dragon dragons = new dragon[n];
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Arrays.sort(dragons, new Comparator<dragon>() {
@Override
public int compare(dragon o1, dragon o2) {
if(o1.strength < o2.strength)
return -1;
else if(o1.strength == o2.strength) {
if(o1.points < o2.points)
return -1;
return 1;
}
return 1;
}
});
for(int i = 0 ; i < n ; i = i + 1) {
System.out.print(dragons[i].strength + " " + dragons[i].points);
}
}
}
Sample input :
2 2
99 1
100 1
java nullpointerexception
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
Why am I getting null pointer while running this program , at line 13 : dragons[i].strength = Integer.parseInt(input[0]);
??
public final class practice {
public static void main(String args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine().split(" ");
int n = Integer.parseInt(input[1]);
int s = Integer.parseInt(input[0]);
class dragon{
int strength; int points;
};
dragon dragons = new dragon[n];
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Arrays.sort(dragons, new Comparator<dragon>() {
@Override
public int compare(dragon o1, dragon o2) {
if(o1.strength < o2.strength)
return -1;
else if(o1.strength == o2.strength) {
if(o1.points < o2.points)
return -1;
return 1;
}
return 1;
}
});
for(int i = 0 ; i < n ; i = i + 1) {
System.out.print(dragons[i].strength + " " + dragons[i].points);
}
}
}
Sample input :
2 2
99 1
100 1
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
java nullpointerexception
java nullpointerexception
edited Nov 22 at 8:12
asked Nov 22 at 8:01
sorenl
82
82
marked as duplicate by Joe C, Wai Ha Lee, Pshemo
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();
}
);
});
});
Nov 22 at 8:14
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 Joe C, Wai Ha Lee, Pshemo
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();
}
);
});
});
Nov 22 at 8:14
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.
2
When you dodragon dragons = new dragon[n]
your array only containsnull
values, you have to populate it with actualdragon
objects. Also by convention, class names should start with an upper-case letter, i.eDragon
.
– Arnaud
Nov 22 at 8:04
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to dodragon dragons = new dragon();
before setting its attributes.
– cactuschibre
Nov 22 at 8:05
1
Create a new dragon, eg.dragons[i] = new dragon();
otherwise the array is full of null.
– matt
Nov 22 at 8:10
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27
add a comment |
2
When you dodragon dragons = new dragon[n]
your array only containsnull
values, you have to populate it with actualdragon
objects. Also by convention, class names should start with an upper-case letter, i.eDragon
.
– Arnaud
Nov 22 at 8:04
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to dodragon dragons = new dragon();
before setting its attributes.
– cactuschibre
Nov 22 at 8:05
1
Create a new dragon, eg.dragons[i] = new dragon();
otherwise the array is full of null.
– matt
Nov 22 at 8:10
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27
2
2
When you do
dragon dragons = new dragon[n]
your array only contains null
values, you have to populate it with actual dragon
objects. Also by convention, class names should start with an upper-case letter, i.e Dragon
.– Arnaud
Nov 22 at 8:04
When you do
dragon dragons = new dragon[n]
your array only contains null
values, you have to populate it with actual dragon
objects. Also by convention, class names should start with an upper-case letter, i.e Dragon
.– Arnaud
Nov 22 at 8:04
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to do
dragon dragons = new dragon();
before setting its attributes.– cactuschibre
Nov 22 at 8:05
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to do
dragon dragons = new dragon();
before setting its attributes.– cactuschibre
Nov 22 at 8:05
1
1
Create a new dragon, eg.
dragons[i] = new dragon();
otherwise the array is full of null.– matt
Nov 22 at 8:10
Create a new dragon, eg.
dragons[i] = new dragon();
otherwise the array is full of null.– matt
Nov 22 at 8:10
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
dragon dragons = new dragon[n];
Keep in mind that this line does not initalize any objects of the type dragon. It only initalizes an array of the size n and type dragon.
To fix this you should create an object of type dragon in your for loop.
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i] = new dragon(); //<-- new line
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Also keep in mind that by convention your class names should start with an capital letter (i.e. Dragon).
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
add a comment |
up vote
0
down vote
The array you have created here dragon dragons = new dragon[n];
has null values.
You haven't added any dragon
Objects
to that one. So when you try to access dragons[i].strength
you are trying to access a attribute of a null value. So it will give you a NullPointerException
add a comment |
up vote
-5
down vote
Maybe you have to write
dragon dragons = new dragon[n];
instead of
dragon dragons = new dragon[n];
3
Both syntaxes mean exactly the same and this is not the reason of theNullPointerException
.
– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the. The latter is C-style, the former is the usual Java-style.
– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
dragon dragons = new dragon[n];
Keep in mind that this line does not initalize any objects of the type dragon. It only initalizes an array of the size n and type dragon.
To fix this you should create an object of type dragon in your for loop.
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i] = new dragon(); //<-- new line
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Also keep in mind that by convention your class names should start with an capital letter (i.e. Dragon).
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
add a comment |
up vote
3
down vote
dragon dragons = new dragon[n];
Keep in mind that this line does not initalize any objects of the type dragon. It only initalizes an array of the size n and type dragon.
To fix this you should create an object of type dragon in your for loop.
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i] = new dragon(); //<-- new line
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Also keep in mind that by convention your class names should start with an capital letter (i.e. Dragon).
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
add a comment |
up vote
3
down vote
up vote
3
down vote
dragon dragons = new dragon[n];
Keep in mind that this line does not initalize any objects of the type dragon. It only initalizes an array of the size n and type dragon.
To fix this you should create an object of type dragon in your for loop.
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i] = new dragon(); //<-- new line
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Also keep in mind that by convention your class names should start with an capital letter (i.e. Dragon).
dragon dragons = new dragon[n];
Keep in mind that this line does not initalize any objects of the type dragon. It only initalizes an array of the size n and type dragon.
To fix this you should create an object of type dragon in your for loop.
for(int i = 0 ; i < n ; i = i + 1) {
input = br.readLine().split(" ");
dragons[i] = new dragon(); //<-- new line
dragons[i].strength = Integer.parseInt(input[0]);
dragons[i].points = Integer.parseInt(input[1]);
}
Also keep in mind that by convention your class names should start with an capital letter (i.e. Dragon).
edited Nov 22 at 9:56
answered Nov 22 at 8:11
D. McDermott
687
687
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
add a comment |
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
@sorenl this seems to be the solution! totally agreed with the answer! you are taking array of Objects of dragon class so you should have the loop making a new instance of dragon and add values to its Field Variables!
– Rizwan atta
Nov 22 at 8:17
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
Another point, seeing how the object creation question has been cleared up, is that you should instantiate your Dragon class fields in a constructor and then use setters to change those fields i.e dragons[i].setStrength() = Integer.parseInt(inpout[0])
– PumpkinBreath
Nov 22 at 8:22
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
@PumpkinBreath Why would you use a getter to set a value? If you want to encapsel the values, you should use a setter: dragons[i].setStrength(Integer.parseInt(input[0]);
– D. McDermott
Nov 22 at 8:24
add a comment |
up vote
0
down vote
The array you have created here dragon dragons = new dragon[n];
has null values.
You haven't added any dragon
Objects
to that one. So when you try to access dragons[i].strength
you are trying to access a attribute of a null value. So it will give you a NullPointerException
add a comment |
up vote
0
down vote
The array you have created here dragon dragons = new dragon[n];
has null values.
You haven't added any dragon
Objects
to that one. So when you try to access dragons[i].strength
you are trying to access a attribute of a null value. So it will give you a NullPointerException
add a comment |
up vote
0
down vote
up vote
0
down vote
The array you have created here dragon dragons = new dragon[n];
has null values.
You haven't added any dragon
Objects
to that one. So when you try to access dragons[i].strength
you are trying to access a attribute of a null value. So it will give you a NullPointerException
The array you have created here dragon dragons = new dragon[n];
has null values.
You haven't added any dragon
Objects
to that one. So when you try to access dragons[i].strength
you are trying to access a attribute of a null value. So it will give you a NullPointerException
answered Nov 22 at 8:10
Sand
725111
725111
add a comment |
add a comment |
up vote
-5
down vote
Maybe you have to write
dragon dragons = new dragon[n];
instead of
dragon dragons = new dragon[n];
3
Both syntaxes mean exactly the same and this is not the reason of theNullPointerException
.
– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the. The latter is C-style, the former is the usual Java-style.
– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
add a comment |
up vote
-5
down vote
Maybe you have to write
dragon dragons = new dragon[n];
instead of
dragon dragons = new dragon[n];
3
Both syntaxes mean exactly the same and this is not the reason of theNullPointerException
.
– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the. The latter is C-style, the former is the usual Java-style.
– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
add a comment |
up vote
-5
down vote
up vote
-5
down vote
Maybe you have to write
dragon dragons = new dragon[n];
instead of
dragon dragons = new dragon[n];
Maybe you have to write
dragon dragons = new dragon[n];
instead of
dragon dragons = new dragon[n];
answered Nov 22 at 8:07
Fabio Canella
13
13
3
Both syntaxes mean exactly the same and this is not the reason of theNullPointerException
.
– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the. The latter is C-style, the former is the usual Java-style.
– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
add a comment |
3
Both syntaxes mean exactly the same and this is not the reason of theNullPointerException
.
– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the. The latter is C-style, the former is the usual Java-style.
– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
3
3
Both syntaxes mean exactly the same and this is not the reason of the
NullPointerException
.– Jesper
Nov 22 at 8:09
Both syntaxes mean exactly the same and this is not the reason of the
NullPointerException
.– Jesper
Nov 22 at 8:09
The Java compiler does not care where you place the
. The latter is C-style, the former is the usual Java-style.– Zabuza
Nov 22 at 8:10
The Java compiler does not care where you place the
. The latter is C-style, the former is the usual Java-style.– Zabuza
Nov 22 at 8:10
Both are same here :)
– Sand
Nov 22 at 8:12
Both are same here :)
– Sand
Nov 22 at 8:12
add a comment |
2
When you do
dragon dragons = new dragon[n]
your array only containsnull
values, you have to populate it with actualdragon
objects. Also by convention, class names should start with an upper-case letter, i.eDragon
.– Arnaud
Nov 22 at 8:04
Your object of class dragon (should begin with upper case 'D') in the loop is not instanciated. You need to do
dragon dragons = new dragon();
before setting its attributes.– cactuschibre
Nov 22 at 8:05
1
Create a new dragon, eg.
dragons[i] = new dragon();
otherwise the array is full of null.– matt
Nov 22 at 8:10
This is a problem where using your debugger would give you the answer much faster than asking SO.
– Peter Lawrey
Nov 22 at 8:27