NullPointerException on ArrayList in java [duplicate] - java

This question already has answers here:
Java array, NullPointerException?
(2 answers)
Closed 7 years ago.
I am writing a simple java code but it gave me error of NullPointerException. My code is
String s="google";
ArrayList ss[]=new ArrayList[2];
ss[0].add(s);
System.out.println(ss[0]);

When you create the array, you do not create each element of the array. This will work:
ss[0] = new ArrayList();
ss[0].add(s);

Related

Kotlin : How to convert Array<List> to arrayof? [duplicate]

This question already has answers here:
Java ArrayList to Kotlin Array
(4 answers)
Closed 1 year ago.
So, I try to make a line chart using AAChartModel. I have ArrayList in my code. But in AAchartModel they used an arrayOf instead of ArrayList.
So My Question is, how do I convert from ArrayList to arrayOf ?
arrayOf() returns just an Array. So you can call arrayList.toTypedArray()

How to fix Following NullPointerException Using java 8? [duplicate]

This question already has answers here:
Idiomatic way to create a Stream from a Nullable object
(7 answers)
Closed 4 years ago.
The following code gives me a NullPointerException. As my examine, it is caused by containerModels being null.
List<DoseDetailMutableDTOToBaseDoseDetailAdapter> adapters =
containerModels.stream()
.map(DoseDetailMutableDTOToBaseDoseDetailAdapter::new)
.collect(Collectors.toList());
How to fix it using java 8?
This could do the trick:
Optional.ofNullable(containerModels)
.orElse(Collections.emptyList())
.stream()
...

Error with array initialization [duplicate]

This question already has answers here:
How is this illegal
(5 answers)
Array error in java [duplicate]
(3 answers)
Error in simple array... How to fix [duplicate]
(1 answer)
Closed 5 years ago.
New to Java, this is part of a practice question from a question in my book where i'm learning java. I think I've traced the error to the array, in particular to the initialization but i'm not sure how to fix it, why isn this correct?
int[][][] arr;
arr= new int[20][][];
arr[0] = new int[1][];
arr[0][0] = new int[10]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When you supply an array initializer expression, you can't specify the array dimensions too.
You can use :
arr[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When i run it in eclipse it says Cannot define dimension expressions when an array initializer is provided., i think that's really clear for an error message. It means you can either specify the dimensions or the initialize the array. But NOT both at the same time.
Change to:
inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};

Correct way to instantiate an array in java? [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 5 years ago.
I am trying to declare and instantiate an array named 'canines' that had 25 elements and will hold 'dog' objects...
Is this the best way to do this or is there another formate that would be more correct?
canines[25] = Dog
Dog[] canines = new Dog[25];
Have a look at some of the many online java resources

how to use an array or an ArrayList with indices bigger than Integer_Maximum_Value? [duplicate]

This question already has answers here:
Is there a longer than int Java List?
(3 answers)
Using a long as ArrayList index in java
(7 answers)
Closed 7 years ago.
I want to have a big ArrayList, so its index would go beyond int, how or what can I use for that?
I already checked that Vector does have the same issue, any ideas?

Categories

Resources