In: Computer Science
My add method is not working to add elements into an arrayList in java.
This is the error message I keep getting:
Exception in thread "main" java.lang.NullPointerException
at assignment1.ArrayBag.add(ArrayBag.java:50)
at assignment1.Main.main(Main.java:21)
BUILD FAILED (total time: 0 seconds)
The following are all the methods I've created.
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment1;
/**
*
* @author Daniela Garcia Toranzo ; PID: 5674893
*/
public class ArrayBag implements Bag {
private int SIZE = 10;
private String[] bag;
private int length;
public void ArrayBag() {
bag = new String[10];
length = 0;
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public void print() {
for (int i = 0; i < bag.length; i++) {
if (bag[i] != null) {
System.out.print(bag[i] + " ");
}
}
}
@Override
public void add(String s) {
if (isFull() == true) {
System.out.println("Arraylist is full, cannot add anything else");;
} else {
bag[length] = s;
length++;
}
}
@Override
public void remove(String s) {
boolean found = false;
String x, y = "";
for (int i = 0; i < length; i++) {
x = bag[i].toString();
if (x.equals(y)) {
found = true;
bag[i] = bag[length - 1];
bag[length - 1] = null;
length--;
}
}
}
@Override
public int count(String s) {
int counter = 0;
for (int i = 0; i < length; i++) {
if (s.equals(bag[i])) {
counter++;
}
}
return counter;
}
private boolean isFull() {
if (length > SIZE) {
System.out.println("ArrayList is Full");
}
return length > SIZE;
}
}
The add(String s) method is correct and it is throwing an error when it is executing the below statement:
bag[length] = s;
I have corrected the error and highlighted the statement which you need to modify. You need to set the size of the string array in the variable declaration section of ArrayBag class.
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment1;
/**
*
* @author Daniela Garcia Toranzo ; PID: 5674893
*/
public class ArrayBag implements Bag {
private int SIZE = 10;
//private String[] bag;
private String[] bag = new String[10];
private int length;
public void ArrayBag() {
bag = new String[10];
length = 0;
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public void print() {
for (int i = 0; i < bag.length; i++) {
if (bag[i] != null) {
System.out.print(bag[i] + " ");
}
}
}
@Override
public void add(String s) {
if (isFull() == true) {
System.out.println("Arraylist is full, cannot add anything else");;
} else {
bag[length] = s;
length++;
}
}
@Override
public void remove(String s) {
boolean found = false;
String x, y = "";
for (int i = 0; i < length; i++) {
x = bag[i].toString();
if (x.equals(y)) {
found = true;
bag[i] = bag[length - 1];
bag[length - 1] = null;
length--;
}
}
}
@Override
public int count(String s) {
int counter = 0;
for (int i = 0; i < length; i++) {
if (s.equals(bag[i])) {
counter++;
}
}
return counter;
}
private boolean isFull() {
if (length > SIZE) {
System.out.println("ArrayList is Full");
}
return length > SIZE;
}
}