I have created a class named "Global Services" which I use to save my data globally and access them in a different activity. But when I am calling the set() method, instead of overview the existing data instead it is appending that data. Below is my code.
I have even tried to remove the instance but still, it is appending the new data instead of overwriting.
import java.util.ArrayList;
import java.util.List;
public class GlobalServices {
private static GlobalServices instance;
String partner, leadsResponse;
List<Leads> assignedList = new ArrayList<>();
List<Leads> unAssignedList = new ArrayList<>();
List<Inventory> listInventory = new ArrayList<>();
private GlobalServices() {}
public static GlobalServices getInstance() {
if (instance == null) {
instance = new GlobalServices();
}
return instance;
}
public static void destory() {
instance = null;
}
public String getPartner() {
return partner;
}
public String getLeadsResponse() {
return leadsResponse;
}
public List<Leads> getAssignedList() {
return assignedList;
}
public List<Leads> getUnAssignedList() {
return unAssignedList;
}
public List<Inventory> getListInventory() {
return listInventory;
}
public void setPartner(String partner) {
this.partner = partner;
}
public void setLeadsResponse(String leadsResponse) {
this.leadsResponse = leadsResponse;
}
public void setAssignedList(List<Leads> assignedList) {
this.assignedList = assignedList;
}
public void setUnAssignedList(List<Leads> unAssignedList) {
this.unAssignedList = unAssignedList;
}
public void setListInventory(List<Inventory> listInventory) {
this.listInventory = listInventory;
}
}
The problem is that you're just assigning new references to your lists in GlobalServices but not creating new lists. This means as soon as you modify this reference from another place in your code, it will be reflected in the GlobalServices list as well. All you have to do is:
public void setAssignedList(List<Leads> assignedList) {
this.assignedList = new ArrayList<>(assignedList);
}
public void setUnAssignedList(List<Leads> unAssignedList) {
this.unAssignedList = new ArrayList<>(unAssignedList);
}
public void setListInventory(List<Inventory> listInventory) {
this.listInventory = new ArrayList<>(listInventory);
}
This way a new copy will be created in memory for each list and the data will be overwritten.
Sorry if I was wrong, but your code here is not a problem.
The problem might come from other part of your application.
The data you set might be the data that extend your current data.
Example you have
GlobalServices instance = GlobalServices.getInstance()
List<Inventory> listInventory1 = new ArrayList<>();
listInventory1.add(new Inventory());
instance.setListInventory(listInventory1); // now your inventory have one item
// In some where else in your project
List<Inventory> listInventory2 = instance.getListInventory(); // lisInventorys.size() equals 1
// Then you add more data to listInventory2 by mistake
listInventory2.add(new Inventory()); // listInventory2.size() equals 2
// Then you set back listInventory2 to your global service
instance.setListInventory(listInventory2); // now your inventory have two item
So, the data had been actually overwrite, it data just been extended by accident.
Related
I'm just looking some another efficient way to pass an object parameter to method.
So I have some method like this:
private void dashboardMenu() {
Dashboard dashboard = new Dashboard();
body.removeAll();
body.add(dashboard);
dashboard.setSize(body.getWidth(), body.getHeight());
dashboard.setVisible(true);
}
private void dataMenu() {
Data data = new Data();
body.removeAll();
body.add(data);
data.setSize(body.getWidth(), body.getHeight());
data.setVisible(true);
}
And I want an efficient method to call between this two method with object parameter (dashboard = new Dashboard(), and data = new Data()).
What I think it should be like this for example:
private void dasboardMenu() {
navigateMenu(Type object);
}
private void dataMenu() {
navigateMenu(Type object);
}
private void navigateMenu(Type object) {
object menu = new object();
body.removeAll();
body.add(menu);
menu.setSize(body.getWidth(), body.getHeight());
menu.setVisible(true);
}
Is it possible to do that?
Please give me an example. I don't even know what keyword should I do.
How about this (assuming your Dashboard and Data are Swing Components)?
private void dashboardMenu() {
navigateMenu(new Dashboard());
}
private void dataMenu() {
navigateMenu(new Data());
}
private void navigateMenu(JComponent c) {
body.removeAll();
body.add(c);
c.setSize(body.getWidth(), body.getHeight());
c.setVisible(true);
}
When running my MVC model application, a Singleton instance of ApplicationModel is created.
When running the main method in my controller class, a List<Shop> is assigned to shops using setShops(). If shopsDefault = null, setShops() will also continue to assign the same List<Shop> to shopsDefault.
So far, so good.
However, when I call sortShopsByName() - which as you can see below uses setShops() - both shops AND shopsDefault become sorted! Why does it not just sort shops as intended?
My ApplicationModel Class...
import java.util.*;
public class ApplicationModel {
//static variables
private static ApplicationModel instance = null;
//instance variables
private List<Shop> shops;
private List<Shop> shopsDefault;
//constructors
private ApplicationModel() {}
//getInstance method
public static ApplicationModel getInstance() {
if (instance == null) {
instance = new ApplicationModel();
}
return instance;
}
//getters and setters
public List<Shop> getShops() {
return shops;
}
public void setShops(List<Shop> shops) {
this.shops = shops;
if (this.shopsDefault == null) {
this.shopsDefault = shops;
}
}
public List<Shop> getShopsDefault() {
return this.shopsDefault;
}
//Shop methods
public void sortShopsByName() {
List<Shop> shops = this.getShops();
Collections.sort(shops);
this.setShops(shops);
}
public void returnShopsToDefaultOrder() {
List<Shop> shopsDefault = this.getShopsDefault();
setShops(shopsDefault);
}
}
Once you assign shops to shposDefault, they both reference the same instance. Changes made to the instance via either instance will, thus, be visible via both references.
If this is not the intended behavior, you could copy the shops list when setting null. E.g.:
public void setShops(List<Shop> shops) {
this.shops = shops;
if (this.shopsDefault == null) {
this.shopsDefault = new ArrayList<>(shops);
}
}
Because it is the same list. If you want different lists, make a copy when you assign it to shopsDefault (i.e. new ArrayList<Shop>(shops)).
In this code you're setting both members to the same list reference:
this.shops = shops;
if (this.shopsDefault == null) {
this.shopsDefault = shops;
}
If you want to separate them use the following:
this.shops = shops;
if (this.shopsDefault == null) {
this.shopsDefault = new ArrayList<Shop>(shops);
}
I am trying to save a custom class containing ArrayLists to SharedPreferences using GSON. Each time I rotate the screen, the activity starts over and the string generated by gson seems to append the ArrayList to the previous string, instead of replacing it.
The SimpleUserValues class is just a class to store user info, with an empty constructor and private ArrayLists together with their corresponding setters and getters.
The code in my activity is as follows:
SimpleUserValues simpleUserValues = SimpleUserValues.newInstance();
simpleUserValues.setTheBooleans(userValues.getTheBooleans());
Gson gson = new Gson();
Type classType = new TypeToken<SimpleUserValues>() {
}.getType();
String insertedJSON = gson.toJson(simpleUserValues, classType);
if (getSharedPreferences(myAppKey, MODE_PRIVATE).contains("JSON")) {
Log.e("insertedJSON ", getSharedPreferences(myAppKey, MODE_PRIVATE)
.getString("JSON", ""));
getSharedPreferences(myAppKey, MODE_PRIVATE).edit().remove("JSON")
.commit();
getSharedPreferences(myAppKey, MODE_PRIVATE).edit()
.putString("JSON", insertedJSON).commit();
} else {
getSharedPreferences(myAppKey, MODE_PRIVATE).edit()
.putString("JSON", insertedJSON).commit();
}
The output I see from Log.e is like this:
insertedJSON﹕ {"theBooleans":[true,true,true,true,true,true],...
insertedJSON﹕ {"theBooleans":[true,true,true,true,true,true,true,true,true,true,true,true],...
insertedJSON﹕ {"theBooleans":[true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true],...
insertedJSON﹕ {"theBooleans":[true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true],...
Any ideas / known issues ? I am even deleting the previously saved string before writing, so I cannot understand how this appending is coming from. The input into SimpleUserValues is always the same. Thanks in advance :)
EDIT: here it is the custom class SimpleUserValues:
public class SimpleUserValues {
private int theCurrentFragment;
private int theCurrentGraph;
private ArrayList<Boolean> theBooleans;
private ArrayList<Integer> theIntegers;
public SimpleUserValues() {
}
public static SimpleUserValues newInstance(){
return new SimpleUserValues();
}
public ArrayList<Boolean> getTheBooleans() {
return theBooleans;
}
public void setTheBooleans(ArrayList<Boolean> theBooleans_) {
theBooleans = theBooleans_;
}
public ArrayList<Integer> getTheIntegers() {
return theIntegers;
}
public void setTheIntegers(ArrayList<Integer> theIntegers_) {
theIntegers = theIntegers_;
}
public int getTheCurrentFragment() {
return theCurrentFragment;
}
public void setTheCurrentFragment(int theCurrentFragment_) {
theCurrentFragment = theCurrentFragment_;
}
public int getTheCurrentGraph() {
return theCurrentGraph;
}
public void setTheCurrentGraph(int theCurrentGraph_) {
theCurrentGraph = theCurrentGraph_;
}
}
I had a bug in the arrays I was saving, adding more rows instead of replacing. Oops. Thanks #Kane for your comment
I have created ArrayList and make setter and getter for it.
In the other class i want to add objects in this array. But my code doesn't works. i think i need to add in method setter for array some other code..
public class DataVar {
private ArrayList<String> arrayLinks = new ArrayList<>();
public ArrayList<String> getArrayLinks() {
return arrayLinks;
}
public void setArrayLinks(ArrayList<String> arrayLinks) {
this.arrayLinks = arrayLinks;
}
}
//Here is another class
public class LinksAd {
public void getAllLinksAd() {
DataVar dataVar = new DataVar();
String link = "href";
dataVar.setArrayLinks(link) }}
Looking at your code you are trying to add a String type, where your code specifies that you are expecting an ArrayList. Assuming you just want to add a string to your arraylist the following will work:
public void setArrayLinks(String arrayLinks) {
this.arrayLinks.add(arrayLinks);
}
You can implement generic setter method.
This is DataVar class:
public class DataVar {
private List<String> itemList=new ArrayList<String>();
public List<String> getItemlist() {
return itemList;
}
public void setItemList(Object list) {
if (list.getClass().equals(String.class)) {
itemList.add((String)list);
}
else if (list.getClass().equals(ArrayList.class)) {
itemList = (ArrayList<String>)list;
}
else {
throw new Exception("Rejected type- You can set String or ArrayList");
}
}
}
And this is calling setter method example:
Main class:
public static void main(String[] args) {
List<String> exampleList = new ArrayList<String>();
exampleList.add("This example");
exampleList.add("belongs to");
String owner = "http://www.javawebservice.com";
DataVar dataVar = new DataVar();
dataVar.setItemList(exampleList);
dataVar.setItemList(owner);
for(String str:dataVar.getItemlist()){
System.out.println(str);
}
}
Output:
This example
belongs to
http://www.javawebservice.com
So, you can set ArrayList, also you can set String.
You could do a mehtod to add an Item like this:
public void addStringToList(String s)
{
arrayLinks.add(s);
}
In LinksAd you have to wirte:
dataVar.addStringToList(link);
I'm trying to fill a jComboBox with objects. I have it working in one class, but in this class it's giving a NullPointerException but the code is almost the same. What am I missing here?
The code I'm using to fill the comboboxes:
I have translated every variable to English and removed some unnescessary stuff. I hope it's more clear for you guys now:
package unive.billing.boundary.clientmanager.frames;
import unive.billing.control.ClientsManager;
import unive.billing.control.InsuranceManager;
/**
*
* #author Forza
*/
public class ClientFrame extends javax.swing.JFrame {
/**
* Creates new form AddClientGUI
*/
private ClientsManager clientmanager;
private InsuranceManager insurancemanager;
public ClientFrame() {
initComponents();
clientmanager = new ClientsManager();
clientmanager.printList();
updateComboBoxCompany();
updateComboBoxInsurance();
}
private ClientsManager clientmanager;
private InsuranceManager insurancemanager;
public ClientFrame() {
initComponents();
clientmanager = new ClientsManager();
clientmanager.printList();
updateComboBoxCompany();
updateComboBoxInsurance();
}
public void updateComboBoxCompany()
{
for (Object object : insurancemanager.getCompanyNames())
{
companyComboBox.addItem(object);
}
}
public void updateComboBoxInsurance()
{
for (Object object : insurancemanager.getPolicyNames())
{
insuranceComboBox.addItem(object);
}
}
Here are the methods used:
public Object[] getCompanyNames()
{
ArrayList<String> cnames = new ArrayList<String>();
for (InsurancesCompany company : insurancecompanyList)
{
cnames.add(company.getCompanyName());
}
return cnames.toArray();
}
public Object[] getPolicyNames()
{
ArrayList<String> vnames = new ArrayList<String>();
for (Insurance insurance : insuranceList)
{
vnames.add(insurance.getPolicyName());
}
return vnames.toArray();
}
This is how my lists are initialized:
public class InsuranceManager {
private String insurancePath;
private String insurancecompanyenPath;
private static List<InsurancesCompany> insurancecompanyList;
private static List<Insurance> insuranceList;
private Insurance currentInsurance;
public InsuranceManager() {
insurancecompanyenPath = "Files/company.txt";
insurancePath = "Files/insurance.txt";
insuranceList = new List<>();
}
public void createNewList()
{
insurancecompanyList = new List<>();
System.out.println("Creates list");
}
public Object[] getCompanyNames()
{
ArrayList<String> cnames = new ArrayList<String>();
for (InsurancesCompany company : insurancecompanyList)
{
cnames.add(company.getCompanyName());
}
return cnames.toArray();
}
public Object[] getPolicyNames()
{
ArrayList<String> vnames = new ArrayList<String>();
for (Insurance insurance : insuranceList)
{
vnames.add(insurance.getPolicyName());
}
return vnames.toArray();
}
Edit: Here's the MainGUI which calls createNewList (maakLijstAan)
private ClientsManager clientsmanager;
private BillingManager billingmanager;
private InsuranceManager insurancemanager;
public MainGUI() {
clientsmanager = new ClientsManager();
clientsmanager.CreateNewList();
insurancemanager = new InsuranceManager();
insurancemanager.CreateNewList();
insurancemanager.loadInsuranceCompanyList();
initComponents();
jMenuItem1.setText("Save clients");
jMenuItem2.setText("Load clients");
jMenuItem3.setText("Exit");
}
You never initialize verzekeringBeheer, therefore you get a NullPointerException when you try to invoke methods on that variable.
You should have somewhere in your constructor, something like this:
verzekeringbeheer = new VerzekeringBeheer();
Also, try to avoid making your code coupled with other parts of your code. For example:
public VerzekeringBeheer() {
...
//verzekeringmaatschappijLijst is never initialized!!!
}
public void maakLijstAan()
{
verzekeringmaatschappijLijst = new Lijst<>();
System.out.println("Maak lijst aan");
}
public Object[] getMaatschappijNamen()
{
ArrayList<String> mnamen = new ArrayList<String>();
// Here you use verzekeringmaatschappijLijst without checking that is not null!!!
for (VerzekeringsMaatschappij maatschappij : verzekeringmaatschappijLijst)
{
mnamen.add(maatschappij.getMaatschappijNaam());
}
return mnamen.toArray();
}
If nobody calls maakLijstAan, you will get a NullPointerException in getMaatschappijNamen. Try to avoid code that is so dependent of external code, in order to run without problems.
all data for JComboBox are stored in ComboBoxModel
set ComboBoxModel for proper Objects type (String, Integer, Icon or simple Object), Java7 implements Generics, there are significant differiences in compare with Java6
all updates (to the JComboBox or its Model) must be done on Event Dispatch Thread
I only see you useing variables but for me they are nit initialized. So they are null and you get a NPE.
So how are verzekeringmaatschappijLijst and verzekeringLijst initialized?