I want to create an array of JEditorPane depending on the size of a String array.
Is there a possibility to create an array of JEditorPane? If yes, how?
Here is an example:
String [] elements = {"0","1","2","3","4"};
JEditorPane ePane [] = new JEditorPane[5];
I want to to put each String element into the certain JEditPane, i.e
JEditorPane[0].setText(elements[0]);
etc. But I get a nullpointerexception when run I run.
Your problem is, that Java initializes a new array with the default value for the given type. In this case it is null, because the JEditorPane inherits from Object.
You cannot call a method on null - that is where the NullPointerException comes from.
The solution: make a loop in which you initialize the JEditorPane-objects in the array.
Then you can do JEditorPane[0].setText(elements[0]);
Related
Let's say I want to use the range function (inside a ForEach loop) in Azure Data Factory to create an array which consists of integers. These integers represent API pages related to some ID which was given to us as a parameter in the ForEach loop.
I would use it like #range(1, int(varMaxApiPages)).
This gives me what I expect; an array of integers:
[1, 2, 3]
But would it be possible to append the related ID to these integers? So the result would be something like: [{"someID", 1},{"someID", 2},{"someID", 3}]?
Such as:
def appendToArray(varMaxApiPages):
arr1 = list(range(varMaxApiPages))
json_array = [];
for item in arr1:
jsonObejct = {"someID",item}
json_array.append(jsonObejct)
for item in json_array:
print(item)
appendToArray(3)
The correct json array should look like this [{"someID": 1},{"someID": 2},{"someID": 3}], we can achieve that. If you don’t want the colon, you can think of a way to replace it.
My debug result is as follows:
I declared 3 array type variables. Variable res is used to review the debug result.
In Set variable1 activity, assign the value to it via #range(1,3).
Then Foreach the arr1.
Inside Foreach activity, we can use Append variable activity, add expression #json(concat('{"someID":',item(),'}')). It will convert json string to json Object and append to the array jsonArray.
Outside Foreach activity, assign the value of array jsonArray to array res to review the result, you can omit this step and use array jsonArray directly.
That's all.
I have a custom class "Book" and I am trying to save two books in an array in the following way:
class ViewController: UIViewController, UITableViewDataSource,
{
testBook.setBookTitle(title: "Harry Potter")
testBook.setbookPage(page: 12)
myBookCollection.append(testBook)
// testBook = Book()
testBook.setBookTitle(title: "testing")
testBook.setbookPage(page: 66)
myBookCollection.append(testBook)
for books in myBookCollection
{
print(books.getBookTitle())
}
}
If I run the above I my books in the array are stored as ["testing","testing"] as opposed to ["Harry Potter","testing"].
However If I uncomment testbook = Book() it works fine. Can someone please explain why this is the case ?
thanks,
Reference vs. value type.
If you define Book as a class, when you add it to array, it does not copy our object. testBook and myBookCollection[0] are holding the same object. When you change either one, the other will be affected. If you add testbook = Book(), then testBook is now pointing to a different object in memory and what you do it it has no impact on myBookCollection[0].
You can change Book from a class (reference type) to a struct (value type). In that case, when you add it to an array, it will add a copy to the array so what you do with testBook after doesn't affect it.
Here's a video from WWDC that demonstrates the exact problem you had: Building Better App with Value Types in Swift
the array doesn’t hold a copy of your Book instance , it simply hold the pointer to the object(book). When you change the title and page you are not changing the array contents, you are changing the object which testbook is pointing to.
however if you uncomment the line testBook = Book() . You create a new instance of Book class and you set the pointer testbook to the newly created object and all the changes from this point is done on the new object and that’s why it works as it should.
And all of this is true if your are using a class in your array , if you use struct that’s another story
I want to know if I can create an array of XYChart.Series<String, Number> on JavaFX?
Because I need to create a XYChart.Series for each person on database, and I tried to make an array of XYChart.Series<String, Number> for my LineChart but I couldn't.
This is what I tried:
List<XYChart.Series<String, Number>> seriesList = new ArrayList<XYChart.Series<String, Number>>();
Because I had no idea how to do it otherways.
Can you please help me?
You can create a array of the raw type and assign it to a variable declared with a generic type.
int size = ...
XYChart.Series<String, Number>[] seriesArray = new XYChart.Series[size];
Update
Array elements still need to be initialized this way since arrays generated this way are filled with null elements.
Java 8 provides a way create & initilize a array using short code via streams API:
XYChart.Series<String, Number>[] seriesArray = Stream.<XYChart.Series<String, Number>>generate(XYChart.Series::new).limit(size).toArray(XYChart.Series[]::new);
I have a string array and I am just initializing it as below in a java file (Constants.java)
public static final String[] MY_ARRAY = {"xyz","pqr","trw","abc"};
I am using this MY_ARRAY in another java file in main method as below:
String [] HEADER = Constants.MY_ARRAY;
// MY_ARRAY is coming as sorted in natural order ("abc","pqr","trw","xyz").
This is an intermittent issue and not happening every time I run my program. I also have another final static String arrays like MY_ARRAY and have different strings. I am facing same issue with all these string arrays. I cant say when exactly this will happen but it happens intermittently.
Thanks in advance.
I've been testing out HashMap recently and I've stumbled upon an interesting problem. I have an array, a3, that I have set some arbitrary values. I then take this array and put it into a HashMap instance map.
My problem is that when I assign an Object instance the value of put("a3", a3), I can't seem to print out the address of the instance.
For example:
HashMap<Object, Object> map = new HashMap<Object, Object>();
int[] a3 = new int[] {1, 2, 3, 4};
map.put("a3", a3);
When I call System.out.println(map.put("a3", a3), I'm given an address in memory.
However, if I create an object instance and then try to print out that instance I get null.
Object copy = map.put("a3", a3);
System.out.println(copy);
Running the above segment gives me a value of null.
Any reason why both these statements, which appear identical, give me different results?
Read the API Documentation for a HashMap. Null is returned if no value existed in the map that matches what you are inserting.
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put%28K,%20V%29
So if "a3" existed in the map already, the you would get a return, but since it doesn't then you get back null. "a3" has been put into the map but you didn't print the map, you printed the return from put().
As #James_Massey say, if you consult official documentation, you can see that the return value for put() method is:
the previous value associated with key, or null if there was no
mapping for key. (A null return can also indicate that the map
previously associated null with key.)