Error in an array, java.lang.NullPointerException [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
So, i'm using an array in my program, and it gives me this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The error is in fourth line of this code:
int n = numbers.length, numBigger10 = 0;
for (int i = 0; i < n; i++)
if (numbers[i] > 10)
numbersBigger10[i] = numbers[i];
numBigger10++;
What can I do to solve?
If you want to know more details about the error please feel free to ask.

The reason you are getting this NullPointerException is because in your if statement you are trying to assign numbers[i] to an index location that is not present in the numbersBigger10 array.
i.e. The two arrays, numbers and numbersBigger10 have different sizes.
edit - It would be helpful to see more code though, specifically the part where you declared numbersBigger10.

Related

Does java not allow two List element modifications within the same iteration? [duplicate]

This question already has answers here:
Concurrent Modification Exception : adding to an ArrayList
(10 answers)
Why is a ConcurrentModificationException thrown and how to debug it
(8 answers)
How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it? [duplicate]
(10 answers)
Closed 3 years ago.
I am having trouble understanding this error.
Error message:
Exception in thread "main" java.util.ConcurrentModificationException
public static int lift(List<String> list) {
int index = 0;
for (String element : list) {
list.remove(index);
list.add(index, element.toUpperCase());
index++;
}
I'm trying to replace list items but I guess removing and adding in the same iteration is considered a concurrent modification? Is that right? I tried element = element.toUppercase() but that wasn't actually modifying the list at all...
Please help! I am a beginner (obviously)

Why am i getting this exception error ? ArrayOutOfBounds? [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 years ago.
I'm trying to check if the integers in an array are increasing. But I'm getting ArrayOutOfBounds exception, and I do not understand why. I'm new to programming.
for (int i =0; i > newArray.length ; i++){
if (newArray[i] > newArray[i+1]){
System.out.println("Not increasing");
}
}
Thank you for your help.
the ArrayOutOfBounds exception happens when you are trying to access a non-existent position in the array.
The problem in your case is the newArray[i+1] which tries to access a position non-existent in your array.
I recommend editing the condition i < newArray.length-1 : There are, of course, other ways to fix this but this is the easiest.
Let’s say your array length is 8 so the positions are from 0 to 7
In the last run of the for loop i=7 and i+1 =8
You don’t have newArray[i+1]
You should put the first if inside a new if
if(i<(newArray.length - 1)){
if (newArray[i] > newArray[i+1]){
System.out.println("Not increasing");
}
}

Compile error java.lang.nullpointerexception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Sorry if I am asking this question that has been already asked, but I am kind of a noob at programming, I tried researching how to fix this bug but I am still having trouble trying to solve it. I am trying to compile this block of code here:
//Default constructor
public Game () {
potAmount = 100;
betAmount = 0;
}
public int getBetFromUser() {
//Introduction to the game
System.out.println("Welcome to Solitaire Dice Game..bet an amount\r\n" +
"\t-if you roll triples you win triple your bet,\r\n" +
"\t-if you roll doubles you win double your bet,\r\n" +
"\t-if you roll 10 or over, you keep your bet\r\n" +
"\t-otherwise you lose your bet\r\n" +
"A bet of 0 ends the game\r\n");
System.out.println("Your current pot is 100");
System.out.println("Enter your bet amount:");
betAmount = keyboard.nextInt();
return betAmount;
And I am trying to call it in my main class but I get this compile error:
Enter your bet amount:
Exception in thread "main" java.lang.NullPointerException
at Game.getBetFromUser(Game.java:26)
at Assign3.main(Assign3.java:9)
This is not a compile error, this is exception thrown in runtime.
You need to initialize keyboard variable before calling keybord.nextLine()
May be you know this thing. But for others I would re-iterate. There is difference between compile time error and run time error. When we compile it, what java compiler does is to check for syntax errors and if no syntax error is present then it would create a .class file out of it. If any syntactical error is there then the code will not compile. Whereas in runtime/execution errors, is an event or situation, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Java ArrayIndexOutOfBoundsException [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
I wrote a Java program below:
int[] tar = {1,2,5};
for(int i=0 ; i<tar.length ; i++)
{
if(tar[i] - tar[i-1] > 2)
{
System.out.print("true");
}
}
why "tar[i] - tar[i-1]" doesn't mean any error? Isn't an error of ArrayIndexOutBoundsException?
It is an Exception and not error (if you mean it by compiler), cause Java knows about your array values only at run time.
Compiler cannot run your code and see that. What if at runtime you provided some good values at right indices ? That is the reason, compiler only checks the compiler level rules. It won't hint you about runtime behaviour.

What's the difference between --i and i--? [duplicate]

This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.

Categories

Resources