I'm creating an ArrayList of type B:
ArrayList<B> cert= new ArrayList<B>;
B a = util.getCerts(path).iterator().next();
this.cert.add(a);
this.certNode(certs);
I'm getting a null pointer exception when I try to set the value:
void certNode(ArrayList<B> certResp)
{
ArrayList<RespNode> exp = new ArrayList<RespNode>();
for (int i = 0; i< certResp.size(); i++) {
exp.get(i).setxxx(certResp.get(i).getxxx());
exp.get(i).setxxx(certResp.get(i).getxxx().toString());
}
}
Any help would be great!
Since you just created the ArrayList instance exp, exp.get(i) doesn't exist, so you can't call exp.get(i).setxxx(...).
EDIT :
Try :
void certNode(ArrayList<B> certResp) {
ArrayList<RespNode> exp = new ArrayList<RespNode>();
for (int i = 0; i< certResp.size(); i++) {
exp.add(certResp.get(i).getxxx());
}
}
It's hard to be sure without knowing the return value of certResp.get(i).getxxx(), but if it returns RespNode, the code above would add that RespNode instance of the list.
The problem is here : exp.get(i). exp is the newly created ArayList, so it's empty, so there is a null at index i
Your list 'exp' is empty. In the for-loop you get a value of null; calling a method from null will cause the NullPointerException.
You have to put values in the list before you try to access them.
void certNode(ArrayList<B> certResp) {
ArrayList<RespNode> exp = new ArrayList<RespNode>();
for (int i = 0; i< certResp.size(); i++) {
exp.add(new RespNode()); // <-----
exp.get(i).setxxx(certResp.get(i).getxxx());
exp.get(i).setxxx(certResp.get(i).getxxx().toString());
}
}
btw you can improve syntax
void certNode(ArrayList<B> certResp) {
ArrayList<RespNode> exp = new ArrayList<RespNode>();
for (B b : certResp){
RespNode resp = new RespNode();
resp.setxxxx(b.getxxxx());
exp.add(resp);
}
}
As you are creating new empty ArrayList i.e []
exp.get(i)
gives you null
Instead you can use add method of ArrayList
void certNode(ArrayList<B> certResp) {
//Here new empty list is created i.e exp=[] //no elements inside
//Suppose your certResp=[B#12312,B#123834] i.e two B class objects
ArrayList<RespNode> exp = new ArrayList<RespNode>();
for (int i = 0; i< certResp.size(); i++) { //Size of certResp=2 as it contains 2 B objects
RespNode res=new RespNode(); //Create a RespNode class as you want to add of certResp ArrayList<RespNode>
res.setxxx(certResp.get(i)); //Take value of xxx method of certResp.get(i) i.e B object xxx method and set into res.setXXX or RespNode class
exp.add(res); //add res object into ArrayList
}
}
Related
This question already has answers here:
Only the last Object is added to the ArrayList
(4 answers)
Add an object to an ArrayList and modify it later
(4 answers)
Closed 7 years ago.
While adding objects to list I was able to see the object is replacing all values in the list.
Please check the below image and notice the code in for loop for duplicates of object in the list.
public static void main(String args[]) {
ArrayList<Modelclass> al = new ArrayList<Modelclass>();
Modelclass obj = new Modelclass();
for (int i = 0; i < 10; i++) {
obj.setName(2 + i);
obj.setRoolno(4 + i);
System.out.println(obj);
//if (!al.equals(obj)) {
al.add(obj);
System.out.println(obj.getName() + "" + obj.getRoolno());
//}
}
}
You are always adding the same
Modelclass obj = new Modelclass();
That you created outside of the for loop. Then, inside the loop, you are modifying the values.
Since it is always a reference to the same object, you are modifying all of the items in the ArrayList.
Try this instead:
for (int i = 0; i < 10; i++) {
Modelclass obj = new Modelclass(); //This is the key to solve it.
obj.setName(2 + i);
obj.setRoolno(4 + i);
System.out.println(obj);
al.add(obj);
System.out.println(obj.getName() + "" + obj.getRoolno());
}
Your obj variable is only being instantiated once, but being added to the list multiple times. Whenever you update the members of obj, you are updating the same piece of memory and thus every list reference shows the same (last added) data.
I guess you are new in Java? Just create new instances in the loop will work.
ArrayList<ModelClass> al = new ArrayList<ModelClass>();
for(int i=0; i<10; i++){
ModelClass obj = new ModelClass();
obj.setName(2+i);
obj.setRoolno(4+i);
al.add(obj);
}
Your problem is that you're referencing the same object cause you created the object before the loop. Should be
public static void main(String args[]) {
ArrayList<Modelclass> al = new ArrayList<Modelclass>();
for (int i = 0; i < 10; i++) {
Modelclass obj = new Modelclass();
obj.setName(2 + i);
obj.setRoolno(4 + i);
System.out.println(obj);
//if (!al.equals(obj)) {
al.add(obj);
System.out.println(obj.getName() + "" + obj.getRoolno());
//}
}
}
Here is just a simple example. Obviously there are simpler ways to set everything up within the constructor, but the arrayList I'm actually working with has already been set up, I just need to change individual sections of it. There HAS to be a way to call a class's functions in ArrayList, but for the life of me I can't figure out how.
import java.util.ArrayList;
public class ArrayTest{
public static void main(String[] args){
//Here's an example of a regular array:
Length[] lArray = new Length[3];
for (int i = 0; i < 3; i++){
lArray[i].setLength(i + 1);
}
//Here's how I was hoping ArrayList would function:
ArrayList<Length> lList = new ArrayList<Length>(3);
for (int i = 0; i < 3; i++){
lList[i].setLength(i + 1);
// --OR--
lList.setLength(i, i + 1);
}
}
}
Here's the length class:
public class Length{
private int length;
Length(){
length = 0;
}
Length(int s){
length = s;
}
public void setLength(int s){
length = s;
}
}
Thanks!
You add elements to the ArrayList with add.
Since it's an ArrayList<Length>, you add Length objects:
lList.add(new Length());
And in your specific loop :
ArrayList<Length> lList = new ArrayList<Length>(3);
for (int i = 0; i < 3; i++){
Length l = new Length();
l.setLength(i+1);
lList.add(l);
}
BTW, the array initialization is also missing an important initialization :
for (int i = 0; i < 3; i++){
lArray[i] = new Length(); // added
lArray[i].setLength(i + 1);
}
If the ArrayList already contains the elements, and you just want to modify them, you can write something like this:
lList.get(i).setLength(i + 1);
assuming that the ArrayList contains the ith element.
You could create a method with your operation/algorithm like
public void foo(){
System.out.println("some algorithm!");
}
inside Length class. This will operate on each instance of Length class.
And for iterating, you can use
ArrayList<Length> lList = new ArrayList<Length>(3);
for (Length l : lList){
l.foo();
}
This will call everything you code inside foo.
I am adding five objects in the list with the help of for loop. I am initializing my object outside the for loop. In the body of for loop i am changing the object setter properties and adding it in the list. The output with this is: It will add five objects but all have the same attributes even after setting the different values for the attribute.
See the following code
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ASD {
public static void main(String args[]) {
List list = new ArrayList<A>();
System.out.println("Before Insert List is " + list);
A obj = new A();
for (int i = 0; i < 5; i++) {
obj.setA(new Random().nextInt(10));
list.add(obj);
}
System.out.println("After Insert List is " + list);
for (int i = 0; i < 5; i++) {
A prObj = (A) list.get(i);
System.out.println("Values are" + prObj.getA());
}
}
}
class A {
int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
If I initialize A's object inside the for loop then it will add five objects and also changes the attribute for the objects. Can anyone explain this behaviour
You have created just one instance and setting it several times inside the for loop.Create a new instance of A inside the for loop, not outside it
for (int i=0;i<5;i++) {
A obj = new A();
obj.setA(new Random().nextInt(10));
list.add(obj);
}
When you are doing this -
A obj = new A();
for (int i=0;i<5;i++) {
obj.setA(new Random().nextInt(10));
list.add(obj);
you are actually adding reference to the same object after changing it's attribute setA.
That way, all of the list elements have reference to the same object (with same value of a).
You need to add new objects if you want to have different values -
for (int i=0;i<5;i++) {
obj = new A(); // new object
obj.setA(new Random().nextInt(10));
list.add(obj);
When I try to reset a list Marked I get a Null Pointer Exception.
The problem must be cause I never said what B and C is. (Boolean B, Integer C) And I don't know how to do this.
Here is a part of my code :
Marked[] marked;
//Create list marked!
public class Marked<B,C>{
public B bool;
public C comp;
}
public Graph(int N)
{
//Fill marked with false and 0
marked = new Marked[N];
for(int i=0;i<N;i++){
marked[i].bool = false;
marked[i].comp=0;
}
Creating an array of Marked doesn't actually initialize the elements in the array:
marked = new Marked[N];
for(int i = 0; i < N; i++) {
marked[i] = new Marked<Boolean, Integer>();
marked[i].bool = false;
marked[i].comp = 0;
}
The statement marked = new Marked[N]; creates a new array of Marked objects with N elements, but does not initialize them. Each element in this array would be null.
You need to manually initialize them by calling the constructor.
So, your for loop should look like this:
for(int i=0;i<N;i++) {
marked[i] = new Marked();
marked[i].bool = false;
marked[i].comp=0;
}
I have a class holding a boolean, and two doubles, and then an array of that class, I need the boolean and doubles to have defaults values of false, 0.0, and 0.0, and then I have function that refers to an element of the array and the moment I try to access an one of the variables from the class it throws an exception saying its null. Here is my class and my function calling it.
public class PanelData {
boolean flag = false;
double tempStart = 0.0;
double tempEnd = 0.0;
}
private PanelData[] panelInfo = new PanelData[115];
private void panelInfoHandler (int i, double timeStart, double timeEnd) throws SQLException
{
if (!panelInfo[i].flag) {
delete();
insert();
panelInfo[i].flag = true;
panelInfo[i].tempStart = timeStart;
panelInfo[i].tempEnd = timeEnd;
}
else if (panelInfo[i].tempStart <= timeStart && panelInfo[i].tempEnd >= timeEnd) {
}
else
{
insert();
panelInfo[i].tempStart = timeStart;
panelInfo[i].tempEnd = timeEnd;
}
}
here is how I call the class.
panelInfoHandler(9, parsedStart, parsedEnd);
new PanelData[115] creates an array of 115 null references. Have you populated panelInfo with references to actual objects?
At a minimum, you then need to loop through that array and create new instances of PanelData for each element in the array, e.g.
for (int i = 0; i < panelInfo.length; i++)
panelInfo[i] = new PanelData();
Your array is full of null elements until you initialize it. To clarify, if you create an array of primitive objects, you get an array of default (i.e. 0) values. However, an array of Objects gets created with null elements.
int[] myIntArray = new int[10]; // 10 default values of 0
Integer[] myIntegerArray = new Integer[10]; // 10 null elements
add this line and then assign the values:
if(panelInfo[i] == null) panelInfo[i] = new PanelInfo();
You need to do something like
for(int i=0;i<115; i++)
{
PanelInfo[i] = new PanelData();
}
(Or whatever is the correct Java Syntax)
public class PanelData {
boolean flag = false;
double tempStart;
double tempEnd;
public PanelData() {
flag = false;
tempStart = 0.0;
tempEnd = 0.0;
}
private PanelData[] panelInfo = new PanelData[115];
for(int i = 0; i < 115; i++)
panelInfo[i] = new PanelData();
Creating the default constructor lets you instantiate the variables with the default values (false, 0.0, 0.0) in this case so you can test if you are getting a vanilla object back or not.