This may sound silly, but, I get this error while casting from List to Array:
ArrayStoreException: null
The code that i am using right now is :
public void onSuccess(List<Agent> resultList) {
Agent[] array = new Agent[resultList.size()];
resultList.toArray(array);
Agent its a class that i have defined with their own field definitions, being all of them private final Strings
But i dont know what i could be missing atm.
Thank you in advance for your help.
Kind regards,
You're probably passing your ArrayList<Agent> to some method which
has just an ArrayList or List parameter (untyped). This method
can pass compilation but mess things up at runtime. Example below.
If you comment out the call to messUp in my example things are OK.
But messUp can add things which are not Agents to your list,
and cause problems this way.
This is my best guess without seeing your code.
Hope it helps.
import java.util.ArrayList;
import java.util.List;
public class Test009 {
public static void main(String[] args) {
List<Agent> resultList = new ArrayList<Agent>();
resultList.add(null);
resultList.add(new Agent1());
resultList.add(new Agent());
messUp(resultList);
test(resultList);
}
private static void messUp(List lst){
lst.add("123");
}
public static void test(List<Agent> resultList){
Agent[] array = new Agent[resultList.size()];
resultList.toArray(array);
System.out.println("done");
}
}
class Agent {
protected int x;
}
class Agent1 extends Agent{
protected int y;
}
Additional Note: OK, well, this doesn't explain
the "null" part of your error message.
So the root cause is somewhat different.
Related
I have a code
class testArrayList
{
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
but when I compiled it, theres an error saying
Im confused why it has an error saying IDENTIFIER EXPECTED or ILLEGAL START OF TYPE tho I already imported import java.util.ArrayList;
You have to change your code as below. You need to add array list inside to the main method.
public class testArrayList {
public static void main(String[] args) {
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
}
You can put logic ONLY inside methods. The main-method is the method that will be executed when u run the program. You can only put declarations and methods inside a class, thats why you get the runtime errors. I won't do a duplicate, just take Anuradha's solution.
auto is not a keyword and it’s fine in java. Change theSyntax of arrayList it to something
List<String> auto= new ArrayList<>();
I'm learning to code and currently I'm trying to clean up my massive code by finally learning classes. I'm telling you this just to give a heads-up my terminology might still be off :) :)
The situation
My code works with "layers" that will be drawn on top of each other
There are two types of layers: video layers and image layers.
Both types of layers are child from parent class "Layer"
They need to be run by creation order
The goal
I want to run a code for each item/object of the class.
current code
import java.util.*;
public class main {
public static void main(String[] args){
// defining the objects
LayerVideo item01 = new LayerVideo();
item01.path = "import/01/";
LayerVideo item02 = new LayerVideo();
item02.path = "import/02/";
LayerImage item03 = new LayerImage();
item03.path = "import/03/";
// here is the main goal:
// to run/call each object from class "Layer"
// "allLayers" does not exist, but that's what I don't know how to do.
allLayers.forEach( item ->{
System.out.println( item.path );
// expected result in console:
// import/01/
// import/02/
// import/03/
});
}
public static class Layer {
}
public static class LayerVideo extends Layer {
public String path;
}
public static class LayerImage extends Layer {
public String path;
}
}
Thoughts
How to get all excising objects from a class
If I have them, how to ID them?, by var name?
Could I sort/filter the objects in a loop?
Two things:
consider to make your classes top level ones. So don't go public static class LayerVideo inside your Main class. If they are that important, the classes should each go into their own java file.
then learn about Java collections to organized object instances. You could define use an ArrayList for example.
Beyond that, the point is probably: if you want common things for two different classes, then your base class needs to have that, like:
public abstract class Layer {
private String path;
public String getPath() { return path; }
public void setPath(String newPath) { path = newPath; }
and then your subclasses simply inherit that behavior.
Then, you simply can add objects with that extend that base type to a collection:
List<Layer> layers = new ArrayList<>();
Layer layer = new VideoLayer();
layers.add(layer);
So, with the help and pointers of #GhostCat I've came to the following working code:
main.java
public class main {
public static void main(String[] args){
// debug to see amount of elements in list
System.out.println( settings.layers.size() );
// Aggregate with .forEach(), but will switch to .stream() to also be able to filter
settings.layers.forEach( layer -> {
System.out.println( layer.path );
// do stuff
});
}
}
settings.java
import java.util.ArrayList;
import java.util.List;
public class settings extends main{
public static List<Layer> layers = new ArrayList<>();
static {
layers.add( new LayerImage(true, "input/01/", new int[] {255,255,255} ) );
layers.add( new LayerImage(false, "input/02/", new int[] {255,0,0} ) );
layers.add( new LayerVideo(true, "input/03/", new int[] {0,0,255} ) );
}
}
Layer.java
public abstract class Layer {
boolean run;
String path;
int[] color;
public Layer(boolean startRun, String startPath, int[] startColor) {
run = startRun;
path = startPath;
color = startColor;
}
}
LayerImage.java
public class LayerImage extends Layer {
public LayerImage( boolean startRun, String startPath, int[] startColor) {
super( startRun, startPath, startColor) ;
}
}
LayerVideo.java
public class LayerVideo extends Layer {
public LayerVideo( boolean startRun, String startPath, int[] startColor) {
super( startRun, startPath, startColor) ;
}
}
In the main.java I'll swap the aggregator .forEach() with .stream() later on. Sounds more flexible and to be able to filter the results in advance seems like a big advantage. (then I can also use the item.run to see if I want to run this layer).
Why is there a settings class? I want to be able to have ALL the settings and variables to set for the outcome in one file. This way I can (I think) quickly use different setting files. Maybe also change it to XML input later. Use a GUI or whatever. (You might think "ah, sweet summer child", I'll cross that bridge when I get there).
The Layer SuperClass will have around the 10 parameters/arguments in the constructor when finished. Feels a lot to change and maintaining for the SubClasses by passing it with super(); And later on make changes. What is I will have around, say, 20 SubClasses. Is there a more efficient way?
Any other pointers on what I can do better in this code above?
All in all a LOT learned today! Thanks everyone and special thanks to #GhostCat
I personally create an static ArrayList from the object for every class and add the objects to it in the constructor (with list.add(this)).
Like:
public class Layer{
static ArrayList<Layer> layerList = new ArrayList<>();
public Layer() {
layerList.add(this);
}
}
Eclipse says: 'chiffres cannot be resolved to a variable', how to fix the call method ?
public class Table {
public static void main(String[] args) {
Tableau1 table = new Tableau1();
table.CreerTable();
table.AfficherTable(chiffres);
}}
part:
and class Tableau1 with array: to declare it
public class Tableau1 {
int [][] chiffres;
int nombre;
public void CreerTable(){
int[][] chiffres= {{11,01,3},
{12,02,4},
{12,03,5}};
//edited
this.chiffres=chiffres;
}
public int[][] AfficherTable(int[][] chiffres){
this.nombre=12;
for(int i=0;i<2;i++){
System.out.println("essai"+chiffres[i][1]);
if(chiffres[i][0]==nombre){System.out.println("ma ligne ="+chiffres[i][0]+","+chiffres[i][1]+","+chiffres[i][2]);
};
}
return chiffres;
}
}
Thanks a lot
You have 3 problems here.
Problem 1 :
1) You method AfficherTable(chiffres) need not to pass an argument, since it is an instance member.
You can simply call
table.AfficherTable();
That solves your problem.
Before doing that problem no 2
Problem 2:
2) You delcared chifferes as a instance member int [][] chiffres;
and you are initializing it's in constructor
public void CreerTable(){
int[][] chiffres= {{11,01,3},
{12,02,4},
{12,03,5}};
}
But if you closely look, you are creating new array again. That won't work, since you are creating new array and forgot your instance member.
Change your constructor to
public void CreerTable(){
chiffres= new int[3][3] {{11,01,3},
{12,02,4},
{12,03,5}};
}
Problem 3 :
After changing that constructor, since you are using it in the same class member, you need not to receive it. Hence you change your method declaration as
public int[][] AfficherTable(){
You'll be fine I guess now.
table.CreerTable();
table.AfficherTable(chiffres);
By resolving chiffres it searches in the Class Table as you don't specify that chiffres comes from Tableau1.
Therefore the solution is:
table.CreerTable();
table.AfficherTable(table.chiffres);
chiffers is neither a local variable of the main method, nor a field of the class Table, that's why the error.
I'm trying to make a very basic software simulation of a router that reads in a text file and acts on the commands and other information given to it. I made a new class called groupclass to hold an ArrayList
package router;
import java.util.ArrayList;
public class groupclass
{
public ArrayList<String> member;
}
Made a HashMap with it as the value
static Map<Integer, groupclass> groupmap = new HashMap<Integer, groupclass>();
And tried this code
private static void groupadd(int groupnum, String address)
{
out.println("debug groupadd");
try
{
groupmap.get(groupnum).member.add(address);
}
catch(NullPointerException e)
{
groupmap.put(groupnum, new groupclass());
groupmap.get(groupnum).member.add(address);
}
}
Which throws a NullPointerException at
groupmap.get(groupnum).member.add(address);
The idea was to make a map and associate a new groupclass object with each group number, and each groupclass would have a list of IPs stored as strings. I'm at a complete loss here, and any tweaks I do cause weirder problems and build errors I don't understand.
Thanks in advance!
Your member variable "member" is not initialized. Add
public member=new ArrayList<>();
i am on the creation of an app in android. its a calculator app. the main activity is where the user could input the equation, and the second activity is where the user can add/edit/delete variables. so i made a new class in another file named Global.java. then i extended it to application, imported everything i need, made s private string, made some public functions, edited the manifest, and initialized it right on my main activity. everything works fine while im only using a string to be passed by the functions but when i started adding what i need, an ArrayList, and made some functions so i could access the list then run it, the app closes. i think its because the arraylist is not allowed to be passed to different classes? am i right or am i just missing something?
please dont downvote my post if i didn't post something needed. i am using aide so there is no log output. code:
Global.java
...
import android.app.*;
import java.util.*;
public class Global extends Application
{
private String s;
public static ArrayList<String> sList;
public String getS() {
return s;
}
public void setS(String ss) {
s=ss;
}
public void add() {
sList.add(s);
}
}
MainActivity.java
...
String s;
...
global=(Global)getApplicationContext();
...
global.setS("jian"); //this one works
global.sList.add("jian"); // this one dont
...
Are you sure you initialized sList, like this:
sList = new ArrayList<String>();
If you didn't, you might want to change its declaration to include this initialization.
public static ArrayList<String> sList = new ArrayList<String>();
Just do
global.add("jian");
since you have an add function to take care of the addition of item to arraylist.
Also, try with this:
public void add(String ss) {
sList.add(ss);
}
You are not instantiating your arraylist.
public static ArrayList<String> sList = new Arraylist<String>();
Also you should read beginner tutorials on Java and android, using a public extension of application like this is a bad idea and you can get log outputs from different apps if Aide doesn't provide that, search play store