Question

In: Computer Science

My add method is not working to add elements into an arrayList in java. This is...

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;

}

}

Solutions

Expert Solution

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;

}

}


Related Solutions

Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT