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.
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 want to create an Array of objects with a specific number of elements in Kotlin, the problem is I don't now the current values for initialization of every object in the declaration, I tried:
var miArreglo = Array<Medico>(20, {null})
in Java, I have this and is exactly what I want, but i need it in Kotlin. :
Medico[] medicos = new Medico[20];
for(int i = 0 ; i < medicos.length; i++){
medicos[i] = new Medico();
}
What would be the Kotlink equivalent of the above Java code?
Also, I tried with:
var misDoctores = arrayOfNulls<medic>(20)
for(i in misDoctores ){
i = medic()
}
But I Android Studio show me the message: "Val cannot be reassigned"
The Kotlin equivalent of that could would be this:
val miArreglo = Array(20) { Medico() }
But I would strongly advice you to using Lists in Kotlin because they are way more flexible. In your case the List would not need to be mutable and thus I would advice something like this:
val miArreglo = List(20) { Medico() }
The two snippets above can be easily explained. The first parameter is obviously the Array or List size as in Java and the second is a lambda function, which is the init { ... } function. The init { ... } function can consist of some kind of operation and the last value will always be the return type and the returned value, i.e. in this case a Medico object.
I also chose to use a val instead of a var because List's and Array's should not be reassigned. If you want to edit your List, please use a MutableList instead.
val miArreglo = MutableList(20) { Medico() }
You can edit this list then, e.g.:
miArreglo.add(Medico())
If you want list of nullable objects, we can do something like this
val fragment : Array<Fragment?> = Array(4) { null }
This question might appear trivial, but I am unable to find a solution to this problem for a few days.
Snippet 1 :
for(int i = 0; i < arr.length; i++){
arr[i] = new Bucket();
}
Snippet 2 :
Arrays.fill(arr, new Bucket());
Code with snippet 1 is executing as expected, but code that includes snippet 2 is not passing all the test cases.
I am expecting both the statements to do the same work internally. But the test cases show it is not. Any help to clarify this will be very helpful.
Think about what they do: in the loop you create a new object on every iteration. In the second you create one object and fill the array with it. They are totally different.
From documentation:
Assigns the specified Object reference to each element of the specified array of Objects.
Arrays.fill() uses the same object all time:
public static void fill(Object[] a, Object val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
So entire array becomes filled with a single instance created once by new Bucket()
I am expecting both the statements to do the same work internally.
Snippet 2 is only creating one Bucket, and adding the same instance to every slot. So they are not the same, in Java 8+ you could use a lambda and something like
IntStream.range(0, arr.length).forEach(x -> arr[x] = new Bucket());
to fill the Bucket[] arr.
i'm a beginner java programmer and i did some research but i cannot understand how to apply it to my program.
when i run my program, i get a NullPointerException as i enter the nickname for the first IP Address. Why?
the isolated line in the code is where i get the Exception.
You are missing this line (before NullPointerException is thrown):
ipAddress[i] = new IPAddress();
You should initialize array's elements before.
You have created an array of Ip Adresses but you never filled in any IP adress.
Here ipAddress[i].nickname = input.next(); you assume that ipAddress[i] holds an IPAdress object and try to set its nickname field to input.next(). But since you haven't added any objects to it, the array, is filled with the default value which is null.
When you allocate an array like this:
IPAddress[] ipAddress = new IPAddress[2];
it creates an array with two slots, but both of the slots have null. You need to put something in each slot before you can use it as an object:
ipAddress[i] = new IPAddress();
ipAddress[i].nickname = input.next();
Inside local_address you are going to get another NPE. You set result to null initially and don't assign an array to it. That's why you're getting a NPE. You can fix this with:
String[][] result = new String[addr.length][]; // instead of null
However, you will also need to assign a String[] for each value of j. If you don't know what count will grow to be, you might consider using a List<String> that can grow automatically for you.
As an aside: I don't know what you're trying to accomplish, but your logic doesn't look correct. Do you really need a two-dimensional String array? It seems like this should be what you want:
static List<String> local_address(IPAddress addr[]) {
List<String> result = new LinkedList<>();
for (int j = 0; j < addr.length; j++) {
IPAddress test = addr[j];
if (test.xx == addr[j + 1].xx & test.yy == addr[j + 1].yy) {
result.add(addr[j + 1].nickname;
}
}
return result;
}
I have a Tuple mock class, whose getString(0) and getString(1) methods are expected to be called n times. Instead of writing something like,
when(tuple.getString(0)).thenReturn(logEntries[0]).thenReturn(logEntries[1])...thenReturn(logEntries[n - 1])
manually, I tried the following:
OngoingStubbing stubbingGetStringZero = when(tuple.getString(0)).thenReturn(serviceRequestKey);
OngoingStubbing stubbingGetStringOne = when(tuple.getString(1)).thenReturn(logEntries[0]);
for (int i = 1; i < n; i++) {
stubbingGetStringZero = stubbingGetStringZero.thenReturn(serviceRequestKey);
stubbingGetStringOne = stubbingGetStringOne.thenReturn(logEntries[i]);
}
The expected result is that all calls to tuple.getString(0) should return the String serviceRequestKey and each call to tuple.getString(1) should return a different String logEntries[i] ie. ith invocation of tuple.getString(1) returns ith element of logEntries array.
However, due to some odd reason, things are getting mixed up, and second invocation to tuple.getString(1) returns the String serviceRequestKey instead of logEntries[1]. What am I missing here?
Well, the right way to do this would be:
import org.mockito.AdditionalAnswers;
String[] logEntry = // Some initialization code
List<String> logEntryList = Arrays.asList(logEntry);
when(tuple.getString(1)).thenAnswer(AdditionalAnswers.returnsElementsOf(logEntryList));
On each invocation, successive elements of logEntry array are returned. Thus, ith invocation of tuple.getString(1) returns ith element of logEntry array.
P.S: The example in documentation of returnsElementsOf (as of this writing) is not updated (it still uses ReturnsElementsOf example): http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/AdditionalAnswers.html#returnsElementsOf(java.util.Collection)it
If I understood well, you want your mock to return different results depending on the invocation (meaning result1 when invoked for the first time, result2 when invoked for the second time etc)- this can be accomplished with such thing
Mockito.when(tuple.getString(0)).thenReturn(serviceRequestKey,logEntries[0],logEntries[1])
With this you will get serviceRequestKey on first invocation, logEntries[0] on second etc...
If you want something more sophisticated, like change the return depending on the param in the method use thenAnswer() as shown here
Not sure I understand Mockito well enough to understand your example, but wouldn't you want something like:
Mockito.when(tuple.getString(0)).thenReturn(serviceRequestKey);
for(int i = 0;i < logEntries.length;i++) {
Mockito.when(tuple.getString(i+1)).thenReturn(logEntries[i]);
}
I know that post is older, but maybe it helps:
OngoingStubbing<Boolean> whenCollectionHasNext = when(mockCollectionStream.hasNext());
for (int i = 0; i < 2; i++) {
whenCollectionHasNext = whenCollectionHasNext.thenReturn(true);
}
whenCollectionHasNext = whenCollectionHasNext.thenReturn(false);