I am very new to java and haven't learnt much at the moment but I have a issue.
I am trying to pass a variable that runs in a thread to another java class.
in the client thread I am running:
UserReg UR = new UserReg();
UR.addUser(userID);
and in the separate java class I have got:
import java.util.ArrayList;
public class UserReg {
ArrayList userList = new ArrayList();
void addUser (String UserID) {
userList.add(UserID);
System.out.println(userList);
}
}
the issue I have is that each time a new thread runs then it overwrites the array list and doesn't add to it.
That is because it has got do with this:
UserReg UR = new UserReg();
UR.addUser(userID);
Each time, it initializes the userReg and hence the ArrayList inside.
What you can probably do is that (temporarily but not a decent solution) would to make the userList static
Related
I am trying to initialize objects in my ArrayList
The following is my code and what it does is that it receives an ArrayList, already defined in the main method. However, it is not executing the readInput method in the code below. readInput is a method declared in another class called customer that takes in keyboard input from the user. Its a complete class BTW. I believe that there is no object in the ArrayList at the moment as i have run some test to see whats the problem. Im new to using Arraylist. Therefoere i am unsure of how to solve this proble.
What custs.set(i, new customer()); does is actually to initialize the object in index.
public static void freeCustomer(ArrayList<customer> custs,
ArrayList<supplement>supps)
{
for(int i=0;i<custs.size();i++)
{
custs.set(i, new customer());
custs.get(i).readInput();
for(int s=0;s<supps.size();s++)
{
supps.set(s, new supplement());
supps.get(s).readInput();
}
}
}
If I understood right,you want to add user input from a customer method.I would use add method.I just made a fast example of add method.From here you can expand to loops,etc.
public static void freeCustomer(ArrayList<customer> custo,customer c)
{
custo.add(new customer(c.readInput()));
}
public static void main(String[] args) {
ArrayList<customer> custo = new ArrayList<customer>();
customer c=new customer("");
freeCustomer(custo,c);
}
}
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 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
I have a class that extends SwingWorker
public class PowerVCImageDeployer extends SwingWorker<Boolean,String>{
protected Boolean doInBackground() throws Exception {
PowerVCImageDeployer imageDeployer = new PowerVCImageDeployer();
imageDeployer.makeAConnection();
imageDeployer.makeNameIdMap();
// more code
return null;
}
public HashMap<String,String> makeNameIdMap(){
System.out.println("I am in make NameIdMap");
VolumeNameGetter idToName = new VolumeNameGetter();
ArrayList<String> list = (ArrayList<String>)volIDMap.keySet();
idNameMap = idToName.volNameGetter(list);
This is the trouble point. The control goes class idToName and it looks as if it doesn't come back. I have fully tested the class VolumeNameGetter using main method. The class is working good. There is no problem with it. So that looks as if once the control goes away from the SwingWorker it is unable to get it back ?? I have posted the output in the last line
System.out.println("The name Id Map is "+idNameMap);
System.out.println("Checking if the control comes back here");
return idNameMap;
}
}
The output of the code is
I am in make NameIdMap
This is troubling me a lot. Any help would be highly appreciated. Thanx in advance.
Debugger Output
and line number 127 is
return voulmeMap;