By using setters and getters class, I am setting values to MusicDetailsObject(pojo class) when setting first value it is set, then setting second value it is set and also replacing first value and so on...... setting values using loop:
for(int k=0;k<songs.length();k++)
{
JSONObject songObject = songs.getJSONObject(k);
//mName.add(songObject.getString("name"));
mObject.setmName(songObject.getString("name"));
//mURL .add(songObject.getString("url"));
mObject.setmURL(songObject.getString("url"));
//mListId .add(songObject.getString("id"));
mObject.setmID(songObject.getString("id"));
mAlbumList.add(mObject);
}
first time setting values as
array1{id = 101
name = Jigarthanda}
When setting value second time it showing like this:
array1{id = 102
name = Mundasupatti}, array1{id = 102
name = Mundasupatti}
so, please let me know how to set an individual value.
Thanks in advance.
The issue is that, inside the loop, you never create a new instance of mObject, so the only instance you have is getting reused.
for(int k=0;k<songs.length();k++){
JSONObject songObject = songs.getJSONObject(k);
mObject = new MyObject() // -> NOW you are assigning values to a different instance.
//mName.add(songObject.getString("name"));
mObject.setmName(songObject.getString("name"));
//mURL .add(songObject.getString("url"));
mObject.setmURL(songObject.getString("url"));
//mListId .add(songObject.getString("id"));
mObject.setmID(songObject.getString("id"));
mAlbumList.add(mObject);
}
Related
I successfully got the Table entries from a SAP system via RFC_GET_TABLE_ENTRIES. It works all fine and lists me all the rows of the table.
My problem right now is that I have no idea how to get a single value out. Usually I would go like codes [x][y] but that doesn't work because it is not a normal two-dimensional-array table but a JCOtable and I have no idea how that works.
The code is a little longer but this is the call itself.
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
JCoFunction function = destination.getRepository().getFunction("RFC_GET_TABLE_ENTRIES");
if (function==null)
throw new RuntimeException("Function not found in SAP.");
function.getImportParameterList().setValue( "MAX_ENTRIES", 30);
function.getImportParameterList().setValue( "TABLE_NAME", "ZTEST_TABLE ");
JCoTable codes = function.getTableParameterList().getTable("ENTRIES");
codes.appendRow();
and this is the console output
System.out.println("RFC_GET_TABLE_ENTRIES");
for (int i = 0; i < 30; i++) {
codes.setRow(i);
System.out.println(codes.getString("WA"));
}
getString actually accepts indexes as well. If you want to retrieve values according to x and y you can do the following
codes.setRow(y);
String value = codes.getString(x); // It can also be getFloat, getInt, etc. depending on the data type,
// or getValue, which gives you an Object
It works similarly to codes[x][y] as if it's an array, but this is not commonly used.
In other cases, you may want to iterate through each single value in the row with JCoRecordFieldIterator.
JCoRecordFieldIterator itr = codes. getRecordFieldIterator();
while(itr.hasNextField()){
JCoRecordField field = itr.nextRecordField();
String value = field.getValue(); // Or getString, getFloat, etc.
// Whatever you want to do with the value
}
I am facing a very strange situation. I add an object to an arrayList in a loop, but it is replaced by the next object. Actually second item is duplicated. ( It replaces the first item as well as inserts another object to the ArrayList.)
This is my code. I have done the debugging and included the comments where needed. Could someone point out why this happens? I am taking the object details from the database and those are working as expected.
public class Serv
{
#Autowired
GrpHeader objGrpHeader;
#Autowired
CompPesoOutgoingMsg objMsg;
#Autowired
OutwardMessage objOutwardMessage;
public List<OutwardMessage> outgoingMessagesAsSingleTrx()
{
List<OutgoingMsg_Obj> trxList = myRepo.getTrx("5");
List<OutwardMessage> myTrxList = new ArrayList<>();
for (OutgoingMsg_Obj outgoingMsg : trxList)
{
BigDecimal trxAmt = outgoingMsg.getIntrBkSttlmAmt().getTrxn_amt();
trxAmt = (trxAmt).divide(new BigDecimal(100));
GrpHeader grpHeader = objGrpHeader;
CompPesoOutgoingMsg outMsg2 = objMsg;
OutwardMessage objOutwardMessage2 = objOutwardMessage;
outgoingMsg.setRmtInf(objRmtInf);
outgoingMsg.setPmtTpInf(objPmtTpInf);
outMsg2.setHeader(grpHeader);
outMsg2.setCdtTrfTxInf(Arrays.asList(outgoingMsg));
objOutwardMessage2.setObjMsg(outMsg2);
**//Here, Correct object details are printed**
log.info("outwardMsg 100 {} ", objOutwardMessage2);
//Add Item to the list
myTrxList.add(objOutwardMessage2);
for (OutwardMessage outwardMsgx : myTrxList)
{
//1. When this loop executed first time, first object details are printed
//2. When printed second time, first added object is no more. And second added object is there twice.
log.info("outwardMsg 101 {} ", outwardMsgx);
}
}
return myTrxList;
}
}
You have a single reference. By setting the objOutwardMessage2to objOutwardMessageyou are just changing the data inside the reference.
Since no new object is created for each iteration, the same objOutwardMessage2 value is getting replaced each time.
Try
OutwardMessage objOutwardMessage2 = new OutwardMessage();
and copy the value of objOutwardMessage to the newly created objOutwardMessage2.
I am a poor programmer but I need some help for an app I have been procrastinating to build. (note sorry for the lack of detail on the first try
I have created an ArrayList of myObjects that have their own properties. When I created the myObject class I created an initializer so that I could add in myObject into an ArrayList of them. I got it working but I am having problems as the properties of the objects are being overwritten as I loop through my code. Here is a simplified example:
myOjbect newMyObject = new myObject
List<myOject> listOfObjects = new ArrayList<myObjet>();
try {
// go through a text file, set some properties of my object...
myArrayValue = some text input //(sorry i didnt want to put the whole code as its sloppy, but it does return an array)
myObject.matrix = myArrayValue; // this value changes as I go through the text file, but in the listOfObects, only the last value is saved to each item in the list
SetStartDate(somestring1); // another constructor/initializer (sorry i forget the correct terminology) I added to the 'myObject' class. This property sets correctly in the list
listOfObjects.add(new myObject(newMyObject));
Then in my class of myObject is have this initializer:
public myObject(myObject other){
matrix = other.matrix;
startDate = other.startDate;
// TODO add all the properties here, so that they get copied
}
public SetStartDate(string inputText){
startdate = inputText // or something like that, I dont have the code on this computer
}
So the startDate property is working, when I loop through the list of items but when I set the matrix property, I always end up with the last property value in my main script as the property value for each item in the list.
Any ideas why the startDate property works fine but not the matrix (which is an array variable)?
thanks
To copy the array elements instead of saving a reference of the array object, you can do this.
matrix = Arrays.copyOf(other.matrix, other.matrix.length);
I had a quick question, Right now I have a method that returns a populated DTO object, in another class I am calling that method, and then trying to get access to some of the values that are on the returned Object. I am having trouble figuring out what the syntax should be to accomplish this. It is returning "result". I am currently getting an error:
"Null pointer access: The variable result can only be null at this location"
The DTO that I am returning contains a List and I want to get access to one of the values on that list. Below is my code snippet. Thank you for your help!
for (Integer i = 0; i < array.size(); i++) {
// System.out.println(array.get(i));
GetAccountRewardSummaryRequest request = new GetAccountRewardSummaryRequest();
AccountRewardSummaryDTO result = null;
request.accountKey = new AccountIdDTO(array.get(i));
RewardServicesImpl rewardServicesImpl = new RewardServicesImpl();
rewardServicesImpl.getAccountRewardSummary(request);
// This will return an AccountRewardSummaryDTO, print out and see if it is returning properly
System.out.println(result.rewards.get(6));
// System.out.println(request.accountKey);
}
It's not clear from your question, but I suspect that this:
rewardServicesImpl.getAccountRewardSummary(request);
should be:
result = rewardServicesImpl.getAccountRewardSummary(request);
If you want to use the value returned from a method, you need to do something with it.
Your code would be clearer if you didn't declare the result variable until you needed it though - and there's no point in using Integer here instead of int. Additionally, unless you really can't reuse the service, you might as well create that once:
RewardServices service = new RewardServicesImpl();
for (int i = 0; i < array.size(); i++) {
GetAccountRewardSummaryRequest request = new GetAccountRewardSummaryRequest();
request.accountKey = new AccountIdDTO(array.get(i));
AccountRewardSummaryDTO result = service.getAccountRewardSummary(request);
System.out.println(result.rewards.get(6));
}
Also, as noted, the fact that your variable called array clearly isn't an array variable is confusing.
So I've been struggling all day today trying to create an instance of a class called 'Sport'.
I've got my code set up so I run the User Interface, which then runs a constructor, which then runs another constructor which loads the Sport values from a text file.
The problem is, the way I'm apparently creating the objects is wrong. Could really use some help.
public static void seperateValues(String sportDetail)
{
String[] sportDetails = sportDetail.split(",");
System.out.println("Adding new sport to the Sport collection");
System.out.println(sportDetail);
/*
for(int i=0; i<sportDetails.length; i++) //just used for testing whether it was splitting correctly
{
System.out.println(sportDetails[i]);
} */
// name,usagefee,insurance,affiliationfees, then court numbers
//Tennis,44,10,93,10,11,12,13,14,15,16
int vlength;
vlength = sportDetail.length();
String[] sportDetailz;
sportDetailz = new String[vlength];
sportDetailz[0] = sportDetails[0]; //name
sportDetailz[1] = sportDetails[1]; //usage fees
sportDetailz[2] = sportDetails[2]; //insurance
sportDetailz[3] = sportDetails[3]; //afflcationfees
String vSportObjectName;
vSportObjectName = sportDetails[0];
String sportinstance;
sportinstance = sportDetails[0]; //this is the name of the sport which I'm hoping each loop around
//it will give a new name to
Sport sportinstance = new Sport(sportDetails);
//System.out.println(Sport.this.name);
}
Error message: variable sportinstance is already defined in method seperateValues(java.lang.String)
http://puu.sh/2zil9
I'm guessing your issue is that you first declare sportinstance as a String. You then try to define it again as a Sport.
Just remove the following lines and try again (as it doesn't look like they actually are used anywhere else):
String sportinstance;
sportinstance = sportDetails[0];
The other option would be to simply rename either one of your instances of sportinstance.
You are trying to define sportinstance as two different datatypes and Java will not allow this. Either change the name of the Sport definition of sportinstance to another variable name or remove the definition.