public class NewClass {
public static void main(String[] args){
int[] a=null;
int p=0;
int i=0;
while(p<11){
a[i]=p;
p++;
i++;
}
}
}
what is wrong here.....here the array can not store data.
public class NewClass {
public static void main(String[] args){
int[] a=null;
int p=0;
int i=0;
while(p<11){
a[i]=p;
p++;
i++;
}
}
}
what is wrong here.....here the array can not store data.
The thread shows two distinct, common mistakes. The first run (where a was left null) produced a NullPointerException because the array reference was never instantiated; and flagged that. The second run allocated a fixed-size array but then attempted to write one element past the last slot, which produced the ArrayIndexOutOfBoundsException that and described.
Key facts to keep in mind: Java arrays are fixed-size and zero-based; declaring a variable does not allocate storage, and accessing an index outside 0..length-1 throws an ArrayIndexOutOfBoundsException. The JVM’s exception message normally shows the illegal index, which makes it easy to spot an off-by-one (the posted trace shows index 10 while a 10-element array only allows 0–9). For details see the Java docs for NullPointerException and ArrayIndexOutOfBoundsException (NullPointerException, ArrayIndexOutOfBoundsException).
Practical options (pick one that fits the goal):
List<Integer> list = new ArrayList<>();
for (int n = 0; n <= 10; n++) list.add(n); int[] arr = java.util.stream.IntStream.rangeClosed(0, 10).toArray(); Quick debugging checklist: read the stack trace to find the exact line and index, print the array length before writes, avoid magic numbers (use a constant or compute the required size), and prefer loops that rely on the array’s actual capacity when possible, as suggested.
Jump to Post— Majestics 84In java u have to dynamically alocate resources for array...
for Line 3 you need
a = new int[size of array];
Jump to Post— stultuske 1,129while(p<11){a better approach would be to change the above to:
while (p < a.length)since you don't always know the length up front. this way, you won't reach an arrayIndexOutOfBoundsException, whether you keep the same number of elements or decide to …
In java u have to dynamically alocate resources for array...
for Line 3 you need
a = new int[size of array];
,
It wil lshow null pointer exception at line 7,
because of line 3,
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
public class NewClass {
public static void main(String[] args){
int[] a;
a=new int[10];
int p=0;
int i=0;
while(p<11){
a[i]=p;
p++;
i++;
}
}
}
Still some problems....oh!
**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at NewClass.main(NewClass.java:17)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)**
The ArrayIndexOutOfBounds is happening due to the while loop.while (p<11)
this executes 0-10.
However our range is from 0-9. (Because we have an array of 10 integers)
change while (p<10) and I guess your code should work fine :)
You have to declare the size of an array before you can put and retrieve data from it.. Otherwise it will create an error..
while(p<11){
a better approach would be to change the above to:
while (p < a.length)
since you don't always know the length up front. this way, you won't reach an arrayIndexOutOfBoundsException, whether you keep the same number of elements or decide to change that number.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.