Checking the value of a regsitry value in java script - java

I have a script in my installer that is checking for the existance of a registry key.
Util.showWarningMessage("Here");
Integer xx = (Integer)WinRegistry.getValue(RegistryRoot.HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Microsoft\\VisualStudio\\11.0\\VC\\Runtimes\\x86", "Installed");
Util.showWarningMessage("Here 2");
if (xx == 1) {
Util.showWarningMessage("return 1");
}
else{
Util.showWarningMessage("here 1");
}
return true;
The script compiles, but when I run the installer I see the first 2 warning messages but not the ones within the if statement. It also seems that the script is returning false(this is a condition statement to see if a certain component is installed).
When I remove the if statement the final return reached as expected.
I would be grateful if someone could explain what is wrong
Thanks in advance

Probably xx is null and the unboxing throws an exception. Try this:
Util.showWarningMessage("xx = " + xx);
to check the value of xx.

Related

For loop runs through an ArrayList of objects and checks their names to display them in GUI but Error Message still shows up

I've decided to programm a search system for finding students and teachers in a school via GUI. It is an OOP and need some tweaking here and there, but there is one issue which doesn't seem logical to me. When I'm searching for a teacher, I have to type there name or surname into a JTextField and press the Search button which runs a method that loops through an ArrayList of teacher-objects and checks if their names match with the one in the Textfield. Then it checks if these teachers have multiple subjects and grades and it goes through nested if-statements. After the teacher is found, their information is displayed on a GUI with several Texfields. Theoretically if the name I typed into the TextField doesn't match one from the teacher objects, a Error Message should pop-up saying the teacher I'm looking for isn't in the system. But even though I type in the correct name and get all the information displayed, it sends me to the Error Message everytime. I tried to fix it with a break statement but that didn't work either. Can someone please help me with this.
Here is the code I'm talking about:
public void lehrerSuche()
{
String lehrername = tfSuchfeldLehrer.getText();
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++)
{
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername))
{
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
break;
}
else
{
switchPanels_3(panelErrorLehrer);
}
}
}
I've uploaded my project to GitHub. Methods and variables are written in German, so I'm really sorry if you can't understand what I have written. If u have questions please hit me up. I use Eclipse to code.
This link should direct you to my GitHub:
https://github.com/Gonzo-CR/Home-Projects.git
If the link doesn't work, look for Gonzo-CR on GitHub and check out my Home-projects repository where I uploaded all the files.
For better undestanding these are the Object oriented classes:
Person(Abstract)
Schueler
Lehrer
Fach
Schulklasse
Spezial
Sprecher
GUI classes:
Suchsystem
Testdaten(A class which generates all my objects)
The problem is likely that if td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) is not true the very first time the loop runs, switchPanels_3(panelErrorLehrer); will be triggered - regardless of whether the condition is met on a later iteration of the loop.
What you need is to check a sentinel value after the loop finishes - e.g.:
bool lehrerGefunden = false;
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++){
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername)){
//etc.
lehrerGefunden = true;
break;
}
}
if(!lehrerGefunden){
switchPanels_3(panelErrorLehrer);
}
That way, you check every entry in the list before deciding whether to show the error.

Why is Eclipse stepping into when I hit step over?

I am using debug mode in Eclipse Oxygen to, as you might guess, debug my code.
I am writing a backtracking algorithm (a recursive function - it calls itself).
In the Backtrack function there is a for loop, and at the end of the for loop, if certain conditions are met, this code is run: Backtrack(csp, index + 1, CopyCSP(currentSolution));.
I'm debugging my code, and I want to go to the next iteration of the for loop, so when I get to this line, I hit "step over." But it steps into, and walks me through the next Backtrack function.
I know for a fact that it is actually the next function, because as you can see, the index variable goes up by one, which happened.
Why is this happening? How can I avoid this and actually step over? If step over doesn't do what I want here, what should I use?
Here's my code for the full function:
private void Backtrack(CSP csp, int index, CSP currentSolution) {
//BREAKPOINT IS HERE
if(index == csp.numVars) {
currentSolution.PrintSolution();
csp.PrintSolution(currentSolution);
solved = true;
return;
}
for(int test = 0; test < csp.MaxDomainSize(); test++) {
if(solved) {
return;
}
if(test < currentSolution.vars[index].domain.size) {
currentSolution.vars[index].value = currentSolution.vars[index].domain.get(test);
}
else {
continue;
}
boolean satisfied = true;
for(int i = 0; i < csp.constraints.size; i++) {
if(!csp.constraints.get(i).Satisfied(currentSolution.vars, index)) {
satisfied = false;
}
}
if(satisfied) {
System.out.println("Variable " + index + " satisfied by " + currentSolution.vars[index].value + ".");
Backtrack(csp, index + 1, CopyCSP(currentSolution));
}
}
}
I've put a comment where the breakpoint is.
I was having a similar problem and found similar solution, posting here for reference.
Problem:
Debug session 'Stepped Into' each line for every 'Step Over (F6)' operation
Solution:
'Run->Remove All Breakpoints'
'Run->Restart'
Result:
'Step Over (F6)' steps over active debug line now as expected.
Sharing for clarity and simplicity if encountered, hopefully this helps.
Following the train of thought that it's the breakpoint...
A breakpoint stops the flow of control in most situations.
Eclipse has options to disable individual breakpoints and disable all breakpoints. Your situation might be right for conditional breakpoints:
Right click on the breakpoint and select breakpoint properties. This will bring up a dialog with "hit count" and "conditional" which works most of the time and is confusing when it doesn't.
If you check "conditional", this will enable a text box where you can write a condition to use the variables to make a true statement. So you could enter "index==1000" and then it would stop when you are 1000 calls deep.
I have not used "hit count" myself.

Android Studio If statement with || and && operator

I'm creating an application which updates users on the score of a football match either in real time or as a final result. At least one score must be inputted in order for the TextView to be updated and the relevant score to be displayed. I'm checking that at least 1 of a pair of EditText fields is not empty using the following code:
if(!(et_current.getText().toString().isEmpty())||(!(et_final.getText().toString().isEmpty()))
&& (!(et_current2.getText().toString().isEmpty())||(!(et_final2.getText().toString().isEmpty()))){
if(!(et_final.getText().toString().isEmpty()))
tv_final.setText(et_final.getText().toString());
else
tv_current.setText(et_current.getText().toString());
if(!(et_final2.getText().toString().isEmpty()))
tv_final2.setText(et_final2.getText().toString());
else
tv_current2.setText(et_current2.getText().toString());
}
I want to be able to set the correct TextView so I have another if statement inside the original if statement to see ensure the correct score is being updated.
When I run the code, I do not seem to be getting past the first if statement. Am I using the correct format or is there an better way to complete these checks?
Thanks!
For readabilities sake, get some variables going
boolean currentEmpty = et_current.getText().toString().isEmpty();
boolean current2Empty = et_current2.getText().toString().isEmpty();
boolean finalEmpty = et_final.getText().toString().isEmpty();
boolean final2Empty = et_final2.getText().toString().isEmpty();
And then your code can be much cleaner. Something like
if( (!currentEmpty || !finalEmpty) || (!current2Empty || !final2Empty)) {
if(finalEmpty) {
tv_current.setText(et_current.getText());
}
else {
tv_final.setText(et_final.getText());
}
if(final2Empty) {
tv_current2.setText(et_current2.getText());
}
else {
tv_final2.setText(et_final2.getText());
}
}
I'm not sure if that is completely correct as the requirement is not entirely clear to me, but it should atleast be a good start to follow what's going on.

Search for accounts using for loops and if statements

I'm having a problem with my program. Currently, I'm making a billing account system.
Basically, I've got a great deal of the system up and running. As a feature, rather than having a user remember their position in an array, a user could carry out actions for their account by entering their account number. So, in other words, they would be prompted to enter an account number and any actions are attributed to that account.
Here is the code I have so far:
intEntry = input.nextInt();
for (count = 0; count <= ACCLIMIT; count++)
{
if (intEntry == NewAccount[count].getAccRefNo() )
{
intSelectedEntry = count;
}//end of if statement
else
{
System.out.println("Invalid ID!");
}//end of else statement
}//end of loop
System.out.println("*******Please enter the amount you wish to deposit*******") ;
valDeposit = getBalanceValidation();
parDepositAmount = Double.valueOf(valDeposit).doubleValue ();
NewAccount[intSelectedEntry].deposit(parDepositAmount);
When I run it, it crashes once I enter the ID number intEntry. It says the error is in the line of the if statement criteria, if that helps.
Please be aware I'm really new to Java, and I'd really appreciate this help explained in a simple way. (Thanks!)
Here is the error message:
Exception in thread "main" java.lang.NullPointerException
at pkgGasAccount.UsingBusinessAccount.main(UsingBusinessAccount.java:106)
Java Result: 1
Line 106 is the first line of the if statement (the criteria)
NewAccount[count] is null.
You should check that NewAccount[count] != null:
if (NewAccount[count]!= null && intEntry == NewAccount[count].getAccRefNo() )
But if you don't expect null values there, I suggest you to check why this happens.
NullPointerException is being thrown, hence I can say that your code is trying to access an array that is not defined or either pointing to a null value (default)
Since there is just one array NewAccount[], hence I would check the declaration of the same.

Eclipse debugger is jumping to the wrong return statement

I've run into a really weird situation. I'm doing the following in Java (through Eclipse Galileo) on the Android 2.1 platform:
// Get gravity & geomagnetic data to return to the caller.
final int SIZE_GRAVITY = 3, SIZE_GEOMAGNETIC = 3;
final float[] NOT_USED = null;
float[] outGravity = new float[SIZE_GRAVITY];
float[] outGeomagnetic = new float[SIZE_GEOMAGNETIC];
final String NO_DATA_COULD_BE_READ = null;
boolean succeeded = SensorManager.getRotationMatrix(NOT_USED, NOT_USED, outGravity, outGeomagnetic);
if (!succeeded)
{
return NO_DATA_COULD_BE_READ;
}
Log.v("Test", "This should be printed - but it isn't!!");
// Prepare the data to return.
final int X = 0, Y = 1, Z = 2;
final String FIELD_SEPARATOR = ",", VECTOR_SEPARATOR = ";";
String returnValue = "" +
outGravity[X] + FIELD_SEPARATOR +
outGravity[Y] + FIELD_SEPARATOR +
outGravity[Z] + VECTOR_SEPARATOR +
outGeomagnetic[X] + FIELD_SEPARATOR +
outGeomagnetic[Y] + FIELD_SEPARATOR +
outGeomagnetic[Z];
// Return data.
return returnValue;
When SensorManager.getRotationMatrix(...) returns false, the Eclipse debugger shows that the if-statement that says if (!succeeded) suddenly jumps to return returnValue;. No exception is thrown and LogCat - even on verbose mode - receives no unusual message. It doesn't even receive the message I put in the code. I tried the obvious clean-and-restart-Eclipse approach and that didn't help. I'm very confused.
The Eclipse debugger is telling me that the second return statement is being called. However, putting additional print statements in shows that the first return statement is actually the one being reached. Perhaps I've stumbled across an Eclipse bug? Or anyone could explain this anomaly?
Looks like succeeded is false. Debugging will make it look like the method jumps to the bottom return when you do any return. Put a log before
return NO_DATA_COULD_BE_READ;
How are you sure that it jumps to the last return? You have a return in the if, this is most likely this one which is called.
I guess, you are in misconception. The control is not reaching the last return statement because the earlier return statement is getting executed.
return NO_DATA_COULD_BE_READ;
Since succeeded is turning out to be false (as you said), the if-condition is satisfied and then it simply returns the null value of the NO_DATA_COULD_BE_READ string, which is inside the if-condition block.
So, Log.v("Test", "This should be printed - but it isn't!!"); is never reached as per observation.

Categories