This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I cannot seem to find why I am getting this error message. I thought I have already instantiated my array in my main.
Exception in thread "main" java.lang.NullPointerException
public class A1ArrayList<E> {
private E[] e;
private int capacity = 0;
public A1ArrayList(){
}
public int size(){
return e.length;
}
public boolean add(E addElement){
e[capacity] = addElement;
capacity = capacity + 1;
return true;
}
public static void main(String[] arg){
A1ArrayList<Object> e = new A1ArrayList<Object>();
e.size();
}
You have to initialize your array. Right now you have a field e that has place for an Array of E. But there's no array in that field! So if you try e[capacity] = addElement; you will try to add something to nothing, which is why you'll get a null pointer.
In your constructor you could use this to initialize the array.
public A1ArrayList(){
E=new E[5];
}
Like that you have an Array where you can store 5 instances of E.
Your Array e is null. Therefore you get a Nullpointer Exception.
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
I'm writing a program which utilizes OOP. The program I am creating is supposed to recruit applicants to a team. In my Team.java, I created a method which is supposed to accept members and add it to the team. This is a snippet of my code:
public int maxMembers;
public Member members[] = new Member[maxMembers];
public int memberCount = 0;
public void addMember(Member newMember) {
members[maxMembers] = newMember;
memberCount++;
}
I have tried this code but the line,
members[maxMembers] = newMember;
keeps throwing an error java.lang.ArrayOutOfBoundsException: 2
I have tried using a for loop in adding a new member but it does not do what I expected. Can anyone assist me in finding a solution?
You have to assign maxMembers a value in the first line, otherwise your array will have 0 elements.
public int maxMembers = 10;
Firstly, you did not initialize the variable maxMembers.Also, in the code, the line members[maxMembers] = newMember; would always put your entry in the end of the array, I think thats not the intended use of your method, public void addMember(Member newMember)
Rewriting your method would look like,
public int maxMembers=somePositiveInteger;
public Member members[] = new Member[maxMembers];
public int memberCount = 0;
public void addMember(Member newMember) {
members[memberCount] = newMember;//here
memberCount++;
}
Initialize the array with a value so that number of elements in array can be decided.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
`this has been bugging me .giving me error.....can anyone tell me how to solve that:
exception in thread "main" StringIndexOutOfBoundsException: String index out of range: 8'
public class Solution {
public static boolean checkit(String input){
int i,j;
boolean s=false;
for(i=0;i<=input.length()-3;i++){
for(j=i+2;j<=input.length()-1;j++)
{
if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))
s=true;
}
}
return s;
}
public static void main(String [] args){
System.out.print(checkit("ABcBAxxs"));
}
}
having
for(j=i+2;j<=input.length()-1;j++)
{
if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))
when j values input.length()-1 then input.charAt(j+1) access at input.length() so out of your String
Out of that, when s become true it is useless to continue to loop because s cannot become false back. You can remove your variable s, replace s=true; by return true; and return s; by return false;
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 3 years ago.
this is the test code I am using to understand how java handles object memory.
public class TestCode {
public static void main(String[] args) {
TestCode obj = new TestCode();
CustomClass cs1 = new CustomClass(5);
obj.updateExistingObj(cs1);
System.out.println(cs1.val);
CustomClass cs2 = new CustomClass(5);
obj.instantiateExistingObj(cs2);
System.out.println(cs2.val);
CustomClass cs3 = null;
obj.updateNullObj(cs3);
System.out.println(cs3.val);
}
void updateExistingObj(CustomClass cs1) {
cs1.val = 9;
}
void instantiateExistingObj(CustomClass cs2) {
cs2 = new CustomClass(9);
}
void updateNullObj(CustomClass cs3) {
cs3 = new CustomClass(9);
}
}
class CustomClass {
int val;
CustomClass next;
CustomClass(int x) { val = x; }
}
The output of the first syso where I am printing cs1.val I am getting expected value which is 9.
The output of the second syso where I am printing cs2.val I am getting 5 as output instead of 9.
The output of the third syso where I am printing cs3.val I am getting a null pointer exception.
Can anybody help me understand what is happening here under the hood? How exactly java handles the memory location when we pass an object as a function parameter? Thanks for helping!!
cs2 and cs3 are local variable, assigning a new value to them have no effect outside of the methods where they are declared.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
class LNode {
LNode next;
int value;
}
public class asasasa {
public static void main(String ar[]) {
LNode list = new LNode(), list2=list;
list.value = 9;
list = list.next;
list.value = 6;
list.next = null;
while (list2.next != null) {
System.out.println(list2.value);
list2 = list2.next;
}
}
}
* Exception in thread "main" java.lang.NullPointerException at asasasa.main(asasasa.java:10)*
Kindly help with the concept and give a simpler code for such linked list
list is never initialized, therefore it is null. Referring to it will always result in a NullPointerException.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
public class chocolatecake
{
int rows,cols;
public chocolatecake(int r, int c)
{
rows = r;
cols = c;
}
private String letterBlock[][];
public void fillBlock(String str)
{
boolean hasExtra;
int tracker=0;
for(int y=0;y<rows;y++)
{
for(int x=0;x<cols;x++)
{
letterBlock[y][x] = str.substring(tracker, tracker+1);
System.out.println(letterBlock[y][x]);
tracker++;
}
}
}
}
SO the point of this program is to take a string and put it into an array first in row major order and then in column major order with a different array size, but im stuck here where the compiler is saying nullpointerexception at line 21
letterBlock[y][x] = str.substring(tracker, tracker+1);
You need to initialize the array letterBlock.
e.g.
String letterBlock[][] = new String[10][2];
Allocate your string array in the constructor:
letterBlock = new String[r][c];
like this:
public chocolatecake(int r, int c)
{
rows = r;
cols = c;
letterBlock = new String[r][c];
// Not like this, it will create a local variable:
// String letterBlock = new String[r][c];
}
private String letterBlock[][];
The other possible reason for a NullPointerException is if str is null. If it is not long enough you will get an IndexOutOfBoundsException.
your rows and cols are assume to be null, this in case you're not calling your chocolatecake function