Why 'variable can be only null at this location'? - java

I have this:
String data[][] = null;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
data[i][j]= "test";
}
}
But it doesn't work. Variable data is null.

Your first line should be
String data[][] = new String[10][10];

because you have to initialize your array first. Consider the array like a "pointer" in C/C++. You have to write something like String data[][] = new String[10][10];

Arrays in java are kind of objects and have to be allocated with new.
Replace
String data[][] = null;
with
String data[][] = new String[10][10];

Of course is data null, you explicitly said so. If you think that
String data[][] = null;
should initialize a 2-dimensional array and each value to null, you are mistaken.
What you need is for example:
String data[][] = new String[10][10]
this initialized a 2-dimensional array with 100 elements, that is to say: an array with 10 elements, each being an array with 10 elements.
An array in Java is an object, just like any other object, and thus has to be initialized with new. data in your example is a reference to an array which itself consists of references to other arrays (= objects).

That is because you've assigned null to it. You need to create an array and assign that to data instead. Try this instead:
String data[][] = new String[10][10];

Array assign value as a Reference type . to to asign any value in it, you have to first create instance of it otherwise it will give error. so you have to write the following :
String data[][] = new String[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
data[i][j]= "test";
}
}

String data[][] is just a reference that can hold a two dimentional string array. since you have not added any object to the reference, it points to null
so do
String data[][] = new String[10][10];
to add an object in to the reference.

you didn't initialize you array. you can define array size as per your need like given example
2d array it's like table row and column you must initialize first row size then column size
here are the example of initialization
data[][] = new String[5][]; // after this you need to define for 2d array like this
data[0] = new String[2];
data[1] = new String[3];
data[2] = new String[1];
as above the column was dynamic you define the column size as much as you want
another one is
data[][] = new String[5][3] then each row has 3 column

Related

how to sort String array Number and bind in array list in android

List<String> list2 =new ArrayList<String>();
int iArr[] = new int[ja.length()];//{"846001","846005","846000","846002","846009"}
Arrays.sort(iArr);
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
I want to sort and bind it in Array list. I want
{"846000", "846001", "846002" ,"846005", "846009"}
but its not sorting according to given logic please suggest me where am doing wrong.
I think the issue is that you are sorting the array before manipulating it. You should move Arrays.sort(iArr) to after the for loop.
List<String> list2 =new ArrayList<String>();
int iArr[] = new int[ja.length()];//{"846001","846005","846000","846002","846009"}
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
Arrays.sort(iArr);
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
The values are accessed as String so convert it to Integers and then sort it. As of now you are sorting it before you populate it properly.
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
Arrays.sort(iArr);
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
Let's simulate how your given logic will be executed when it comes to the processor.
Line 1: initialized an empty string type ArrayList
Line 2: initialized an empty int array, with the length of ja.length() long
Line 3: you sort the empty int array
Line 4 to line 7: assign Pincode to your int array sequentially from the json
We have no idea what those json object stored, but apparently they are not sorted. When they get copied to your int array, the int are not sorted as well.
Move line 3 after the line 4 to 7 for loop should do the job. You should sort the array when there is some element inside. Sort an empty has no effect.

String from ArrayList to two dimensional array using Scanner

I'm using Eclipse and I'm trying to take a string:
1 2 3 4
out of an ArrayList:
ArrayList strings = new ArrayList<>();
And store each number into a two dimensional array:
int size = (int) strings.get(0); // Represents the number of rows
int stringList[][] = new int[size][];
// Store results in a two dimensional array
for(int i = 0; i < size; i++){
int index = i + 1;
String fish = (String) strings.get(index);
Scanner npt = new Scanner(fish);
for(int j = 0; npt.hasNext(); j++) {
size[i][j] = Integer.parseInt(npt.next());
}
}
This is the section that is causing the error:
// ERROR: The type of the expression must be an array type but it resolved to int
size[i][j] = Integer.parseInt(npt.next());
strings is a raw type. Let's start by fixing that1,
List<String> strings = new ArrayList<>();
Then you can use Integer.parseInt(String) parse2 the String to an int like
int size = Intger.parseInt(strings.get(0));
Then there's no need for a cast in
String fish = (String) strings.get(index);
You can use
String fish = strings.get(index);
etc.
1 And program to the List interface.
2 Not cast.
You probably meant
stringList[i][j] = Integer.parseInt(npt.next());
though that wouldn't work either, unless you properly initialize the stringList array.
You would have to initialize stringList[i] (for each i) with something like stringList[i] = new String[someLength]; but I'm not sure which length you should use.

Show different data in 2 separate JTables

The Array data[][] stores the right values inside the loop.
Then I add the array to an ArrayList, but it's like all objects inside the ArrayList are being updated when I change the value of my data array after that.
How can I store the value of each Object[][] separately?
for (int i = 0; i < Materia.length; i++) {
for (int j = 0; j < Materia[i].Aluno.length; j++) {
data[j][0] = Materia[i].Aluno[j].Nome;
System.out.println(data[j][1] = Materia[i].Aluno[j].Nome);//checking outpit, its displaying the correct data I want
data[j][1] = Materia[i].Aluno[j].nota;
data[j][2] = Materia[i].Aluno[j].frequencia;
}
tabs.add(data); //arraylist to storer object data
tabela[i] = new JTable((Object[][]) tabs.get(i), Names);//populate default table model
conteudo2[i] = new JPanel();
conteudo2[i].add(new JLabel(Materia[i].Nome));
conteudo2[i].add(new JScrollPane(tabela[i]));
}
You add your data array (of arrays) to the tab-List.
Since you don't create a new object before adding it to the arrayList, it's the same object, and therefore you also update the contents in the data-Array, when you update the object in the tab-List.
You need to create a new data-Array, before adding it to the list.
You can achieve this by creating a new 2d-Array with the method System.arrayCopy before you add this new object to you tabs-List:
Object[][] myDataCopy = new Object[data.length][];
for(int i = 0; i < data.length; i++) {
myDataCopy[i] = new XXYY[d.length];
System.arraycopy(data[i], 0, myDataCopy[i], 0, data[i].length);
}

Number of columns in 2d array declaration

I want to read a text file and insert it into a 2d array. Here is my code.
List<String> list2 = new ArrayList<String>();
String thisLine = null;
while ((thisLine = input1.readLine()) != null) {
list2.add(thisLine);
}
double[][] X;
X = new double[list2.size()][];
String[] temp;
String delims = ",";
for (int k1 = 0; k1 <= (X.length - 1); k1++) {
String line = input.readLine();
temp = line.split(delims);
for (int i = 0; i < temp.length; i++) {
X[k1][i] = Double.parseDouble(temp[i]);
}
}
The problem is that it returns nothing and as soon as I specify the number of columns in 2d array declaration line, everything is fine. Based on my java knowledge, the number of columns in 2d array declaration is not necessary and in my project (Since we apply different datasets), the number of columns is not specified.
A 2d array is basically an array of arrays. Let's call it an outer array consisting of inner arrays.
When initializing a 2d array, it's true that you only have to declare the length of the outer array. I.e. the number of rows. By doing this, only the outer array will be initialized. The inner arrays are yet to be initialized.
In fact, these two arrays will contain the exact same elements:
double[][] array1 = new double[3][];
double[][] array2 = {null, null, null};
To initialize an inner array, you could do one of these:
array1[0] = new double[4];
array1[0] = new double[] {1.0, 3.14, 42.0, 2.718};
In your case, you could simply put this in your code to initialize the inner arrays:
temp = line.split(delims);
X[k1] = new double[temp.length]; //<- Add this line
X = new double[list2.size()][];
You have now created a jagged array, which is an array of arrays, bound by list2.size().
X[k1][i] = Double.parseDouble(temp[i]);
You have attempted to dereference a spot in the jagged array which does not exist. This is bad.
If the number of columns in your spec isn't specified, then it's provided either by the length of the arguments you parse, or implicitly by the element(s) you attempt to place into that location.
This would work, since it defines an array, which is what the jagged array needs...
X[k1][i] = new double[] {Double.parseDouble(temp[i])};
...but it would only give you a two-dimensional array effective as new Double[list2.size()][1].
Considering that you make use of String[] temp, perhaps instead, you want to use that as the condition on your jagged array? Here's a quick and dirty way to do it that involves more variables than I'd like, but would work:
String[] temp;
double[] tempDouble;
for (int k1 = 0; k1 <= (X.length - 1); k1++) {
temp = input.readline().split(delims);
tempDouble = new double[temp.length];
for(int i = 0; i < temp.length; i++) {
tempDouble[i] = Double.parseDouble(temp[i]);
}
// a bit further down, you can use the array as part of your jagged array.
X[k1] = tempDouble;
}

Java array with empty second brackets

In Java you can do this
int[][] i = new int[10][];
Does this just create 10 empty arrays of int? Does it have other implications?
It creates a 10-entry array of int[]. Each of those 10 array references will initially be null. You'd then need to create them (and because Java doesn't have true multidimensional arrays, each of those 10 int[] entries can be of any length).
So for instance:
int i[][] = new int [10][];
i[0] = new int[42];
i[1] = new int[17];
// ...and so on
Executing your code creates an array of size 10, each element of which can hold a reference to a int[], but which are all initialized to null.
In order to use the int[]s, you would have to create new int[] for each of the element, something like this:
for (int n = 0; n < 10; n++)
i[n] = new int[10]; // make them as large as you need
Yes, it does; however, each of those arrays are null. You have to then initialize each of those sub-arrays, by saying int[10][0] = new int[MY_SIZE], or something similar. You can have arrays with different lengths inside the main array; for example, this code would work:
int[][] i = new int[10][];
for(int ind = 0; ind<10;ind++){
i[ind]=new int[ind];
}
It is just an array of arrays.
Here you create ten new int[0] arrays. You have to manually initialize it, it's useful when you don't need square matrix:
int[][] array = new int[10][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[i];
}
If you need square matrix you can do:
int[][] array = new int[10][10];
And it will be initialized with default values.
That is just the declaration, you need to initialize it. The 10 arrays would be null initially.

Categories

Resources