Add Arraylists together created by a method call - java

I know that there are a few question already in this forum relating to my question, but none of them really seems the help me.
Since I am new to Coding I am still trying to figure out what exactly getClass() and getMethod() calls help me with.
What I want to accomplish:
// init:
List<Preview> listPreview;
List<Preview> listTemp;
// now create the Lists (from a Database)
listPreview = dbHelper.getPreview("Hero", "Axe");
listTemp = dbHelper.getPreview("Hero", "Beastmaster");
// now I want to add ListTemp to ListPreview
Class myClass = listPreview.getClass();
Method m = myClass.getDeclaredMethod("add", new Class[] {Object.class});
m.invoke(listTemp, 2);
The Problem:
Obviously this is not working right now, but I think the idea is pretty straight forward. I want to add listTemp to listPreview. The getDeclaredMethod is already considered a undeclared exception I do not really understand why.

If you want to add two list one after another just use this:
listPreview.addAll(listTemp);

This is relatively simple. Why don't you use listPreview.addAll(listTemp);. This will add all the elements in listTemp to listPreview.

If you want to add the elements of List with your approach, use the below code.
Class myClass = listPreview.getClass();
Method m = myClass.getDeclaredMethod("addAll", Collection.class);
m.invoke(listPreview, listTemp);
OR
For a simpler way, you can use
listPreview.addAll(listTemp);

The error is
getDeclaredMethod is already considered a undeclared exception
Which means there are unreporteds exception must be caught or declared to be thrown.
so below is a complete sample:
try {
Class myClass = listPreview.getClass();
Method m = myClass.getDeclaredMethod("addAll", Collection.class);
m.invoke(listPreview, listTemp);
}
catch (Throwable e) {
System.err.println(e);
}

Related

Java: reading from File folder and adding each to ArrayList

Consider this piece of code:
private static ArrayList<Level> levels = new ArrayList<>();
static {
try (Stream<Path> paths = Files.walk(Paths.get("levels"))) {
paths
.filter(Files::isRegularFile)
.forEach(levels.add(new Level(file))); //CAN'T DO!!
} catch (IOException e) {
e.printStackTrace();
}
}
I think the code pretty much says what I'm trying to do. I found this piece of code somewhere and I tried to apply it, to create Level objects from folder with files such as level1.txt, level2.txt, etc. The Level class takes a File as argument.
The forEach method gives out a SyntaxError.
What are the "::" in the filter method?
Why is the path, fallowed by a new line and 2 methods? Never seen such a thing before.
How can I make this work properly?
The following explains well what is :: (double colon) operator since Java 8
Code can be run written on several lines, but this is same as the following. Your calling the method filter then calling forEach on the object returned by filter
paths.filter(Files::isRegularFile).forEach(levels.add(new Level(file)));
Get it work, you need to define file variable this is done with lambda functions
paths.filter(Files::isRegularFile).forEach(file -> levels.add(new Level(file)));
NOT POSSIBLE because walks() throws IOException
In case you don't need the try/catch you can use Collectors to directly build the list
private static ArrayList<Level> levels = Files.walk(Paths.get("levels"))
.filter(Files::isRegularFile)
.map(file -> new Level(file))
.collect(Collectors.toList());

Android NullPointerException - Debugging a specific line

If I have a line like this:
var.getSomething().getSomethingElse().setNewValue(stuff.getValue().getWhatever());
If that line creates a NullPointerException, is there any way of finding out which method is returning a null value?
I believe I was able to split the line at every dot and get the exception showing which line was failing. But I can't get that to work anymore (maybe I remember incorrectly).
Is the only good debugging possibility to write it like this?
a = var.getSomething();
b = a.getSomehingElse();
c = stuff.getValue();
d = c.getWhatever();
b.setNewValue(d);
With this I should be able to easily see where the exception happens. But it feels inefficient and ugly to write this way.
I use Android Studio. Used Eclipse before but moved to Android Studio some time ago.
You might want to put every part into "Watches":
But I'm pretty sure that both Eclipse and Android Studio would let you inspect the content by just a selection of the part you' re interested in (if you are in debug mode)
The best I can advice for you is to use #Nullable and #NonNull annotations for all methods with return values. It would not help you to get line where null pointer is but would help to prevent such situations in future.
So if method may return null and you have it in call sequence you will get warning from Android Studio about this. In this case it is better to break sequence and check for null.
For example:
private static class Seq {
private final Random rand = new Random();
#NonNull
public Seq nonNull() {
return new Seq();
}
#Nullable
public Seq nullable() {
return rand.nextInt() % 100 > 50 ? new Seq() : null;
}
}
If you write new Seq().nonNull().nonNull().nullable().nonNull(); you will get warning from IDE:
Method invocation `new Seq().nonNull().nonNull().nullable().nonNull()` may produce 'java.lang.NullPointerException'
The best solution in this case is to change code like so:
Seq seq = new Seq().nonNull().nonNull().nullable();
if (seq != null) {
seq.nonNull();
}
Don't forget to add it into Gradle build script
compile 'com.android.support:support-annotations:22.+'
I am not positive on the way you are doing it. This makes your code tightly coupled and not unit testable.
var.getSomething().getSomethingElse().setNewValue(stuff.getValue().getWhatever());
Instead do something like
var.getSomething();
that get something internally does whatever you are doing as a part of
getSomethingElse().setNewValue(stuff.getValue().getWhatever())
In the same way getSomethingElse() should perform whatever you are doing as a part of
setNewValue(stuff.getValue().getWhatever())

BCEL update exceptions table

I want to change a method using BCEL. But I do not know how to update the Exception table. Here's simplified code:
ConstantPoolGen poolGen = classGen.getConstantPool();
InstructionList iList = new InstructionList(method.getCode().getCode());
MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
for (InstructionHandle handle : iList.getInstructionHandles().clone()) {
if (I_WANT_TO_WRAP_IT(handle)) {
iList.insert(handle, MAKE_WRAPPER(handle));
iList.delete(handle);
}
}
classGen.removeMethod(method);
newMethodGen.setMaxStack();
newMethodGen.setMaxLocals();
classGen.addMethod(newMethodGen.getMethod());
After this bytecode is properly modified but exception table is unchanged with leads to ClassFormatError because exception table points to nonexisting PC. Any idea how to deal with this?
Normally you don’t need to deal with this as BCEL should take care of it. It seems to me that your mistake is to use a different instruction list than MethodGen. So you’re modifying the underlying code but the offsets are not processed correctly.
Try to use
MethodGen newMethodGen = new MethodGen(method, classGen.getClassName(), poolGen);
InstructionList iList = newMethodGen.getInstructionList();
to ensure that the same list is used by your code and MethodGen.

JEditorPane - setText from ArrayList<String> contents?

I have read through the JEditorPane Docs, from what I can understand you simply need to editorpane.setText(String value); however I am quite new to java and this solution does not work with my code. I think I am missing something obvious but completely out of ideas.
I have created a new tab with this class that extends JEditorPane, this class is designed to open the contents of the file, put them on an array, reverse the array (so latest entry is on the top) then display this list in the JEditorPane (using JeditorPane because I need to make the save url's into hyperlinks),
public class HistoryPane extends JEditorPane{
ArrayList<String> historyToSort = new ArrayList<String>();
public HistoryPane(){
setEditable(false);
historySort();
}
public void historySort() {
try (BufferedReader reader = new BufferedReader(new FileReader("BrowserHistory.txt")))
{
String currentLine;
String newLine = new String("\n");
while ((currentLine = reader.readLine()) != null) {
historyToSort.add(currentLine + newLine);
}
} catch (IOException e) {
e.printStackTrace();
}
Collections.reverse(historyToSort);
System.out.println(historyToSort);
}
{
}
private void displayHistory(){
String sorted = historyToSort.toString();
***** HistoryPane.setText(String sorted); <<<------ PROBLEM SYNTAX.*****
}
}
I have tried multiple different entries into the setText() parenthesis with no luck. What am I missing? Thank You.
NOTE:
This class won't compile because it is reliant on another class (I can't paste all of it) but this code sits within a tabbed pane created by my main class:
Error Message:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Syntax error on token "setText", Identifier expected after this token
Return type for the method is missing
This method requires a body instead of a semicolon
OK, despite the fact that you haven't read the error message, it seems you're really a newbie, so I'll help.
HistoryPane.setText(String sorted);
The above isn't valid Java. A method invocation takes a list of arguments, without a type.
HistoryPane.setText(sorted);
Now that is a valid method invocation. But it tries to invoke a static method called setText() of the class HistoryPane. What you want is to invoke the instance method setText() on the current object. So the valid syntax is
this.setText(sorted);
or simply
setText(sorted);
That should solve this particular compilation error. Don't try to run your app before every compilation error, listed in the Problems view of Eclipse, is fixed.
Note that the above line won't do what you want it to do, but I'll let you investigate what you should do instead.
My advice: don't try using Swing, which is quite a complex beast, if you don't even know how to call a method yet. Start with very simple Java exercises, not involving any GUI, until you're familiar with the Java syntax, and understand how to read, understand and fix basic compilation problems.

How do I create a List of generic arrays in Java?

I've written a class which accepts a generic type, and I'm trying to create an array list of generic arrays within it. I understand that Java can't create generic arrays, but I also know there are workarounds. Is there a way the below code can work, or am I barking up the wrong tree?
public class IterableContainer<T extends IterableItem> {
private T[] itemArray;
// how can i get this following line to work?
private List<T[]> items = new ArrayList<T[10]>();
public IterableContainer() {
... etc ...
Ignore past here - turns out it was an IDE issue.
Left in for continuity of questions and answers.
EDIT:
This also doesn't work:
private List<T[]> items = new ArrayList<T[]>();
with the error:
Syntax error on token ">", VariableDeclaratorId expected after this token
"... barking up the wrong tree..., use a List<List<T>>. Using raw arrays in Java is almost always a code smell, there is no reason not to use the proper collection classes.
It works just fine, you just can't use the T[10] declaration as the length of an array doesn't affect its type.
i.e.
... = new ArrayList<T[]>();
Not saying it's a great idea, but it should be possible with the same restrictions on generic arrays as always. Creating stuff to put in your list will give you a headache.
private List<T[]> items = new ArrayList<T[]>();
works fine in my machine
When you say "I'm developing for mobile devices" ....are you targeting j2me? There is no support for generics in j2metargetng
This is a valid declaration in java (according to spec) and compiles just fine with javac as others have commented.
public class IterableContainer<T extends IterableItem> {
private T[] itemArray;
private List<T[]> items = new ArrayList<T[]>();// valid
..........
}
I believe the error you are seeing is not emitted from Eclipse, possibly coming from an Android SDK configured in Eclipse. If you create a Java Project in Eclipse, this code should work just fine. If you use this in an Android Project in Eclipse, you are likely to run into this one. I had this error when running this code from an Android project :
# guarantee(_name_index != 0 && _signature_index != 0) failed: bad constant pool index for fieldDescriptor
Sounds like you are restricted in an Android project, unfortunately.
You have not defined T in this code.
If you are creating a generic class, you need to write:
public class <T extends IterableItem> IterableContainer...
The next problem in your code is that you are trying to initilize items of ArrayList during its construction. It is impossible. You should rather write:
private List<T[]> items = new ArrayList<T[]>();

Categories

Resources