If I happen to use a few lines of code in the same method as the construction of the called variables, it works, but if I would create another method that would just do the same operation, copy-pasted and then just call the other method inside it, it wouldn't work.
private void initElements(){
TextView ageValue[] =
{
(TextView)this.findViewById(R.id.age1Value),
(TextView)this.findViewById(R.id.age2Value),
(TextView)this.findViewById(R.id.age3Value)
};
TextView ageText[] =
{
(TextView)this.findViewById(R.id.age1Text),
(TextView)this.findViewById(R.id.age2Text),
(TextView)this.findViewById(R.id.age3Text)
};
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
//updateValues();
}
private void updateValues(){
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
}
If I would uncomment the updateValues() function, the program wouldn't run, even though the same code would be executed in the function before. Moreover, debugging would lead after this statement:
ageValue[i].setText(stringValues[i]);
to this error
I've tried rebuilding and cleaning the program and reinstalling it. I tried it on a virtual device and my real smartphone. Same error over and over. (Moreover, it sometimes says that "Source code does not match the bytecode" when I hover on the error in the picture, but sometimes it also doesn't." Also tried the "this." keyword everywhere, but no success.
I am just baffled how this could happen. Maybe the method does not get some sort of right memory reference because it is not directly next to the declaration but I mean the two statements before the "error statement" work perfectly fine, despite them having the same nature. It would be great if you could like enlighten me why this does not work, thanks!
TextView ageValue[] = new TextView[3];
TextView ageText[] = new TextView[3];
int intValues[] = new int[3];
String stringValues[] = new String[3];
CalculateDates a = new CalculateDates();
This is the declaration of the used variables. (That's in the beginning of the class)
You need to use the global ageValue[] and ageText[] for that class.
private void initElements(){
ageValue[0] =(TextView)this.findViewById(R.id.age1Value);
ageValue[1] =(TextView)this.findViewById(R.id.age2Value);
ageValue[2] =(TextView)this.findViewById(R.id.age3Value);
ageText[0] = (TextView)this.findViewById(R.id.age1Text);
ageText[1] = (TextView)this.findViewById(R.id.age2Text);
ageText[2] = (TextView)this.findViewById(R.id.age3Text);
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
Do not declare the TextViews inside the method also.
You were creating different instances of those textViews in initElements() method and the global TextViews were still null
Related
Im using a method in my FXMLController to check if radio buttons are selected and storing them in a bollean array. I then will be calling this method in another class to get the boolean values to see which are selected to perform perfrom different actions using if statements accordingly.
public boolean[] GetSelectedOption() {
boolean[] OptionsValue = new boolean[2];
OptionsValue[0] = StripHtmlOption.isSelected();
OptionsValue[1] = StripHtmlOption.isSelected();
return OptionsValue;
}
Is this the right way of storing checkbox/radiobutton options? Is there a better way of doing this? As I can imagine this isn't very object orientated? Im a java beginner.
Here is the code where im trying to call the options/boolean values in another class. This code doesnt work though.
public String StripHtml(String html, String DomainName) {
FXMLController FetchOptions = new FXMLController();
boolean[] OptionsValue = FetchOptions.GetSelectedOption();
OptionsValue[0] = StripHtmlvalue;
OptionsValue[1] = = StripLinkValue;
if (Boolean.TRUE.equals(StripHtmlvalue)) {
System.out.println("Some code will go here");
} else {
}
return Stringyettobemade;
}
OptionsValue[0] = StripHtmlvalue;
OptionsValue[1] = = StripLinkValue;
At first please use lower case variable names.
At seccond why do you overwrite the values?
I think what you want is to GET the values so the right code will be:
StripHtmlvalue = optionValue[0];
and what should this do : = =?
This should work
boolean[] optionsValue ={stripHtmlvalue.isSelected(),stripLinkValue.isSelected()};
I am working with a priority queue in Java for the first time and I can't for the life of me understand what I am doing that is leading to the exception. I'm attempting to implement an ant colony type solution to the traveling salesman problem. The following is the only code being called for my AntColony class.
public AntColony(TSPInstance p) {
PriorityQueue<Ant> ants = new PriorityQueue<Ant>(new AntComparator());
size = p.getDimension();
for (int i = 0; i < size; i++) {
ants.offer(new Ant(p));
}
shortestTour = Integer.MAX_VALUE;
}
public void nextMove() {
ants.poll();
}
The code that I'm running afterwards just as a test is as follows (just in a main method).
AntColony a = new AntColony(p);
a.nextMove();
The a.nextMove() throws a NullPointerException at the ants.poll() part, but yet if I change the constructor to (for debugging purposes)
public AntColony(TSPInstance p) {
PriorityQueue<Ant> ants = new PriorityQueue<Ant>(new AntComparator());
size = p.getDimension();
for (int i = 0; i < size; i++) {
ants.offer(new Ant(p));
}
ants.poll(); //ADDED THIS
shortestTour = Integer.MAX_VALUE;
}
and then just do
AntColony a = new AntColony(p);
I don't get an exception. I'm struggling to understand how I'm getting an exception from ants.poll(), but yet when I call it from the constructor everything works. Any help with this would be appreciated. There's a lot of code for various things in this project, so I didn't think uploading it all would help anybody so let me know if there's something I should include, but I don't see how the problem could lie outside these two bits of code.
Added: Actual exception
Exception in thread "main" java.lang.NullPointerException
at data_structures.AntColony.nextMove(AntColony.java:25) (the ants.poll() part)
at algorithms.ACTest.main(ACTest.java:6) The a.nextMove() part
The ants variable in your AntColony constructor is a local variable. So when you exit the constructor, it no longer exists. Apparently the ants variable that your nextMove method is calling, is a class member.
You need to change your constructor to have:
// initialize the class member, not a local instance.
ants = new PriorityQueue<Ant>(new AntComparator());
You can just remove the PriorityQueue declaration in your AntColony constructor.
public AntColony(TSPInstance p) {
ants = new PriorityQueue<Ant>(new AntComparator());
size = p.getDimension();
...
}
UPDATE: The cause for your NullPointerException is that you are not initializing your ants property in your constructor but you are creating a new local ants instead. So the ants object in nextMove method has the same value as you provided in your class level declaration, which it's probably null.
This is my code snippet:
public NaturalNumberTuple toSet()
{
int newTuple[] = new int[tuple.length];
boolean checkIfYouHadToRemoveSomething = false;
for(int i : newTuple){
newTuple[i] = tuple[i];
}
for(int i : newTuple){
for(int j : tuple){
if(newTuple[i] == tuple[j]){
NaturalNumberTuple placeholderTuple = remove(tuple[j]);
newTuple[i] = tuple[j];
checkIfYouHadToRemoveSomething = true;
}
}
}
if(checkIfYouHadToRemoveSomething){
return placeholderTuple;//Problem
} else {
return new NaturalNumberTuple(tuple);
}
}
The method is returning a new NaturalNumberTuple without the given Number (here tuple[j]).
My toSet() method should give me the same Array as I'm giving to it but with only one occurrence per number.
My problem is in the line marked with (//Problem).
The Problem is that placeholderTuple is not defined as a variable. I know it isn't, but if I write at the beginning of my method:
NaturalNumberTuple placeholderTuple;
and at the line where I originally defined my placeholderTuple:
placeholderTuple = remove(..);
it gives me an error that placeholderTuple may not been initialized yet.
I know why I'm getting those errors but I really don't know how to fix that.
If anyone is trying to optimize my code with ArrayLists, please don't because I'm not allowed to use them (not sure if they would help here but at other code snippets they would).
At the start of your method, write :
NaturalNumberTuple placeholderTuple = null;
This will keep this variable visible till the end of the method, and initialize it to a default value.
Then, inside the loop yo change :
NaturalNumberTuple placeholderTuple = remove(tuple[j]);
to
placeholderTuple = remove(tuple[j]);
Declare and initialize it in the begining of the code as:
NaturalNumberTuple placeholderTuple = null;
And in your loop, just initialize it without redefining like placeholderTuple = remove(tuple[j]); and it should work.
I think your code will always return the last entry of newTuple which is similar to tuple, so it doesnt makes sense to me. If you just want to remove very first match, you could do without two loop like:
placeholderTuple = remove(tuple[0]);
Having trouble filling the grid. Everytime I do it I get a stackoverflow error. Here is my current code :
public void removeSelfFromGrid() {
Grid<Actor> grid = getGrid();
int rows = grid.getNumRows();
int cols = grid.getNumCols();
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
Location loc = new Location(i, j);
laugh = new CKiller();
laugh.putSelfInGrid(grid, loc);
}
}
}
and here is the constructor if needed
public CKiller()
{
Color c = null;
setColor(c);
getGrid();
getLocation();
location = new ArrayList<Location>();
location.add(getLocation());
setDirection(direction);
}
And here is the error (part of it, too big to post all. it's just those 2 statements repeated):
java.lang.StackOverflowError
at info.gridworld.actor.Actor.putSelfInGrid(Actor.java:123)
at CKiller.removeSelfFromGrid(CKiller.java:120)
It's saying this is the problem
laugh.putSelfInGrid(grid, loc);
Go through the following:
-Are you defining laugh prior to the removeSelfFromGrid() method call? It doesn't have a type specified before it.
-Should the variable location not be an ArrayList? It might be a Location object.
-Is the int direction defined already?
-Why are you calling getGrid() and getLocation()? They aren't doing anything beneficial.
-Does CKiller inherit the putSelfInGrid() method from the Actor class?
Please include the full code of the CKiller class as well as the main class that contains removeSelfFromGrid().
I think your problem is that you overrode the removeSelfFromGrid() method. You should have created a new method, such as fillGrid().
When an actor calls putSelfInGrid(), if there is currently another actor in that Location it calls removeSelfFromGrid(), which you overrode to fill every Location on the Grid with an actor. If there are any other actors on the grid they will then call removeSelfFromGrid(), which leads to again filling the grid, etc.
Just fix the code in removeSelfFromGrid(), put it in a new method and restore the previous code, and you should be good.
I have such piece of code:
DataTableFactory<Object> TempDataTableFactory = new DataTableFactory<Object>();
DataTable<Object> tempDataTable = TempDataTableFactory.getInstance();
tempDataTable = dataTable;
ExecutedArguments e = new ExecutedArguments();
e.setDataTable(tempDataTable);
e.setExecutedCommand(cmd);
stack.addNewExecutedCommand(e);
result = operation.execute();
Now I just want to keep the olddataTable before execution. When I debug my code till the line result = operation.execute(); there is no problem. In that line I change the dataTable. But because tempDataTable points to dataTable it also changes. But I don't want tempDataTable to change. How can I do this?
If it supports cloning, I would use tempDataTable.clone(). Otherwise you'll have to implement a copy constructor.