# Testing double null

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• May 2nd, 2014, 10:35 AM
javaStooge
Testing double null
I need a little advice on how to execute a particular class. I've created a Kinematics class that will execute and calculate all the functions pertaining to kinematic equations. In the main class, I will have the user provide the data in JTextField. Now I need to test which JTextfields (variables) were left empty. I plan to use a number of if/else statements :
Code :

`if(distanceI != null && distanceF != null)`
The problem with this is that these variables are double and therefore cannot be tested as null. I read a few things about Double but I really don't see how I'm supposed convert each double variable to Double, is that even possible?

I know this code is going to take quite a bit of time to write and will be cumbersome since I need to manipulate each equation to solve for the missing variable, but figuring this out would be a big step in completing this program.

Code :

```public class Kinematics { double distanceI, distanceF, velocityI, velocityF, velocityAverage, acceleration; public Kinematics(){   //v2 = v1 + a*dt if(distanceI != null && distanceF != null){   } else{   }     } public void setDistanceI(double distanceI){ this.distanceI = distanceI; } public void setDistanceF(double distanceF){ this.distanceF = distanceF; } public void setVelocityI(double velocityI){ this.velocityI = velocityI; } public void setVelocityF(double velocityF){ this.velocityF = velocityF; } public void setVelocityAverage(double velocityAverage){ this.velocityAverage = velocityAverage; } public void setAcceleration(double acceleration){ this.acceleration = acceleration; } public double getDistanceI(){ return distanceI; } public double getDistanceF(){ return distanceF; } public double getVelocityI(){ return velocityI; } public double getVelocityF(){ return velocityF; } public double getVelocityAverage(){ return velocityAverage; } public double getAcceleration(){ return acceleration; } }```
• May 2nd, 2014, 10:39 AM
Norm
Re: Testing double null
What do you want to test for? A primitive will never have a null value.
• May 2nd, 2014, 10:44 AM
javaStooge
Re: Testing double null
When I set the restrictions for the if/else statements, the program needs to know which variable to solve for. So for instance, F = ma:
Code :

```if(m != null && a!= null) F = m*a; else if(F!=null && m!= null) a = F / m; else().....```

Do you think that is the best way to do this?
• May 2nd, 2014, 10:46 AM
Norm
Re: Testing double null
Perhaps the caller of the method could sort out what computation to do and call an appropriate method to solve it.
• May 2nd, 2014, 11:32 AM
javaStooge
Re: Testing double null
The method would still need to test whether or not a double is null...no? I had considered making a method for each equation, but since there are slight variations in each of them I thought I could contain them within the same method...
• May 2nd, 2014, 11:39 AM
Norm
Re: Testing double null
Quote:

test whether or not a double is null
A primitive like double will never have a null value.
• May 2nd, 2014, 11:45 AM
javaStooge
Re: Testing double null
Quote:

Originally Posted by Norm
A primitive like double will never have a null value.

Yes, I know that, as I mentioned in my first post. I'm not sure what you are suggesting I can do with the method to "sort out what computation to do".

Would this be attached to the calculate JButton some how?
• May 2nd, 2014, 11:57 AM
Norm
Re: Testing double null
Quote:

test which JTextfields (variables) were left empty.
Test the String that is returned by the getText method.
• May 2nd, 2014, 01:37 PM
javaStooge
Re: Testing double null
Thank you, I took your advice and found some good stuff online. I'm just trying to get my thoughts down in code (what you see below), but after that, I started thinking about the method you were talking about. Should I create a single "checkJTF" method that will perform the actions of the "for" loop below, for each textfield array (e.g. kinematics, dynamics, circuits..etc.) and include the try/catch. . Then I can just call the method inside the actionListener and from there pass the values to the class to be calculated?

My concern with doing it this way is that if I used a single method to check the JTF that it will include all the empty JTF's from each JPanel. So in other words, there would be the empty textfield for voltage, current, and resistors, when the user is actually just trying to to find acceleration.

Also, when I tried to assign the empty JTF to unknown -- what type do I make it?

Code :

```calculate.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){   for(int i = 0; i < kinematicsJT.length ; i++){ if(kinematicsJT[i].getText().isEmpty()){ String unknown = kinematicsJT[i]; } } try{ /*double distanceI = Double.parseDouble(dI.getText()); double distanceF = Double.parseDouble(d0.getText()); double v0 = Double.parseDouble(velocityI.getText()); double v1 = Double.parseDouble(velocityF.getText()); double vA = Double.parseDouble(velocityAverage.getText()); double aA = Double.parseDouble(accelerationAverage.getText()); */ } catch(NumberFormatException ex){ JOptionPane.showConfirmDialog(null, "Numbers Only"); } } });```
• May 2nd, 2014, 01:45 PM
Norm
Re: Testing double null
Quote:

user is actually just trying to to find acceleration.
Would the actionlistener know what the user wants to find and be able to see if the required data has been entered in some textfields? If any required data is missing, issue an error message and go back to the user for correction.
• May 2nd, 2014, 02:04 PM
javaStooge
Re: Testing double null
My thinking was that, as you know, one equation with one unknown, so when the program executes the empty JTF would be assigned the unknown variable. The kinematic class contains the get/set methods, and those values that have been checked and parsed (check method) would be passed to a "calculation" method that contains if/else statements each containing the equations to solve for particular unknown.

Attachment 2910

The logic makes sense, but the code is another story.
• May 2nd, 2014, 02:15 PM
Norm
Re: Testing double null
Where can a missing entry be detected? How can that information be passed to a method for processing?
• May 2nd, 2014, 02:32 PM
javaStooge
Re: Testing double null
That's why I had if(kinematicsJT[i].getText().isEmpty()) so that it would then assign the empty JTF a variable "unknown". I would then use set method from kinematic class to assign the unknown in the kinematic class.

Is any of what I'm talking about even possible, or am I heading down a dark path? I mean there must be a way for this to work.
• May 2nd, 2014, 02:39 PM
Norm
Re: Testing double null
If you passed wrapper class objects: Double vs double, the value could be null.
• May 2nd, 2014, 02:54 PM
javaStooge
Re: Testing double null
I have never used wrapper classes....but I was thinking, what if I assigned an integer number to an "unknown" variable depending on which JTF in the array was left blank. This would tell the switch/case statement in the kinematic class which equation to use for the calculation. May not be elegant but it should work..right?
• May 2nd, 2014, 02:57 PM
Norm
Re: Testing double null
What info do you need to pass to the method? What are the ways to pass it?
If you use an object to pass a value to a method, then a value of null would show the data is missing.
Another way would be to use a variable to flag which of the other variables have no value. For example a boolean array with true/false values for which variables are defined and which are not defined.
callTheMethod(var1, var2, var3, boolArray);
boolArray would have 3 elements corresponding to var1, var2 and var3
• May 2nd, 2014, 04:07 PM
javaStooge
Re: Testing double null
Norm, I hope I am on the right path here...

Method I created to check JTF and assign unknown. It's the only thing I could really understand and work with, so hopefully it's right. The calculate AL is just a rough draft to see if this approach will work and verify I can get a result. Problem, when I try to invoke the method using the kinematics object, this error shows up:
HTML Code:

```cannot find symbol   symbol:  method checkJTF(JTextField[])   location: variable kinematics of type Kinematics```
Code :

``` public void checkJTF(JTextField[] a){ for (int i = 0 ; i < a.length ; i++){ if(a[i].getText().isEmpty()){ int unknown = i; kinematics.setUnknown(unknown); } } }```

Calculate ActionListener:

Code :

```calculate.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ kinematics.checkJTF(kinematicsJT); try{ float distanceI = Float.parseFloat(dI.getText()); kinematics.setDistanceI(distanceI); double distanceF = Double.parseDouble(d0.getText()); double v0 = Double.parseDouble(velocityI.getText()); double v1 = Double.parseDouble(velocityF.getText()); double vA = Double.parseDouble(velocityAverage.getText()); double aA = Double.parseDouble(accelerationAverage.getText());   } catch(NumberFormatException ex){ JOptionPane.showConfirmDialog(null, "Numbers Only"); } } });```

Not sure why the float distance and float time would not work when I tried to use them in the equations.
This is the switch/case I will use in the kinematic class:

Code :

```public Kinematics() { System.out.println(unknown); //float distance = distanceF - distanceI; //float time = timeF - timeI;   if (unknown >= 0 && unknown <= 6) { switch (unknown) { case 1: velocityF = velocityI + acceleration * (timeF - timeI); case 2: distanceF = distanceI + ((velocityI + velocityF) / 2) * (timeF - timeI); case 3: velocityF = (float) Math.sqrt(velocityI * velocityI) + 2 * acceleration * (distanceF - distanceI); case 4: distanceF = (float) (velocityF * (timeF - timeI) + 0.5 * (acceleration * ((timeF - timeI) * (timeF - timeI)))); case 5: velocityAverage = (distanceF - distanceI) / (timeF - timeI); } } }```
• May 2nd, 2014, 04:11 PM
Norm
Re: Testing double null
Quote:

cannot find symbol
symbol: method checkJTF(JTextField[])
The compiler can not find a definition for the method named in the error message. Where is that method define?
Is it in scope where the code tries to use it?

The code snippets don't show enough.
• May 2nd, 2014, 04:23 PM
javaStooge
Re: Testing double null
This is both classes in their entirety. Hopefully this will work. Thanks a lot for help.

Code :

```package physicspractice;   import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*;   public class Frame extends JFrame{   //Objects final Kinematics kinematics = new Kinematics();   public Frame() throws IOException { super("PhysAssist");   // Buttons JButton kinematic = new JButton("Kinematic"); kinematic.setMnemonic(KeyEvent.VK_K); JButton dynamics = new JButton("Dynamics"); dynamics.setMnemonic(KeyEvent.VK_D); JButton power = new JButton("Power and Energy"); power.setMnemonic(KeyEvent.VK_E); JButton wave = new JButton("Wave"); wave.setMnemonic(KeyEvent.VK_W); JButton sound = new JButton("Sound"); sound.setMnemonic(KeyEvent.VK_S); JButton circuit = new JButton("Circuit"); circuit.setMnemonic(KeyEvent.VK_C);   // Execute Buttons JButton calculate = new JButton("Calculate"); calculate.setMnemonic(KeyEvent.VK_ENTER); calculate.setFont(new java.awt.Font("Georgia", Font.BOLD, 16)); calculate.setForeground(Color.BLUE); JButton back = new JButton("Back"); back.setMnemonic(KeyEvent.VK_ESCAPE); back.setFont(new java.awt.Font("Georgia", Font.ITALIC, 16)); back.setForeground(Color.RED);   // Button Array Characteristics final JButton[] buttonArray = { kinematic, dynamics, power, wave, sound, circuit }; for (int i = 0; i < buttonArray.length; i++) { buttonArray[i].setFont(new java.awt.Font("Georgia", Font.BOLD, 16)); buttonArray[i].setForeground(Color.darkGray); buttonArray[i].setBackground(Color.GRAY); }   // Panels final JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout());   final JPanel executePanel = new JPanel(); executePanel.setLayout(new FlowLayout()); executePanel.add(back); executePanel.add(calculate);   final JPanel sidePanel = new JPanel(); sidePanel.setLayout(new BorderLayout());   final JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(6, 1)); buttonPanel.setBackground(Color.DARK_GRAY);   // Kinematic Pane and Contents final JPanel kinematicPane = new JPanel(new GridLayout(6, 2)); kinematicPane.add(new JLabel("Initial Distance")); final JTextField dI = new JTextField(); kinematicPane.add(dI); kinematicPane.add(new JLabel("Final Distance")); final JTextField d0 = new JTextField(); kinematicPane.add(d0); kinematicPane.add(new JLabel("Initial Velocity")); final JTextField velocityI = new JTextField(); kinematicPane.add(velocityI); kinematicPane.add(new JLabel("Final Velocity")); final JTextField velocityF = new JTextField(); kinematicPane.add(velocityF); kinematicPane.add(new JLabel("Average Velocity")); final JTextField velocityAverage = new JTextField(); kinematicPane.add(velocityAverage); kinematicPane.add(new JLabel("Acceleration")); final JTextField accelerationAverage = new JTextField(); kinematicPane.add(accelerationAverage); final JTextField [] kinematicsJT = {dI, d0, velocityI, velocityF, velocityAverage, accelerationAverage};       // Dynamics Pane final JPanel dynamicsPane = new JPanel(new GridLayout(6, 2)); dynamicsPane.add(new JLabel("Force")); final JTextField force = new JTextField(); dynamicsPane.add(force); dynamicsPane.add(new JLabel("Mass One")); final JTextField massOne = new JTextField(); dynamicsPane.add(massOne); dynamicsPane.add(new JLabel("Mass One")); final JTextField massTwo = new JTextField(); dynamicsPane.add(massTwo); dynamicsPane.add(new JLabel("Radius One")); final JTextField radiusOne = new JTextField(); dynamicsPane.add(radiusOne); dynamicsPane.add(new JLabel("Radius Two")); final JTextField radiusTwo = new JTextField(); dynamicsPane.add(radiusTwo); dynamicsPane.add(new JLabel("Normal Friction")); final JTextField frictionNormal = new JTextField(); dynamicsPane.add(frictionNormal);   // Power, Work, Energy Pane final JPanel powerPane = new JPanel(new GridLayout(7, 2)); powerPane.add(new JLabel("Work")); final JTextField work = new JTextField(); powerPane.add(work); powerPane.add(new JLabel("Energy")); final JTextField energy = new JTextField(); powerPane.add(energy); powerPane.add(new JLabel("Power")); final JTextField powerP = new JTextField(); powerPane.add(power); powerPane.add(new JLabel("Mass")); final JTextField mass = new JTextField(); powerPane.add(mass); powerPane.add(new JLabel("Initial Time")); final JTextField timeOne = new JTextField(); powerPane.add(timeOne); powerPane.add(new JLabel("Final Time")); final JTextField timeTwo = new JTextField(); powerPane.add(timeTwo); powerPane.add(new JLabel("Velocity")); final JTextField velocity = new JTextField(); powerPane.add(velocity); powerPane.add(new JLabel("Force")); final JTextField powerForce = new JTextField(); powerPane.add(powerForce);   final JPanel wavePane = new JPanel();   final JPanel soundPane = new JPanel();   final JPanel circuitPane = new JPanel(new GridLayout(3, 2)); circuitPane.add(new JLabel("Voltage")); final JTextField voltage = new JTextField(); circuitPane.add(voltage); circuitPane.add(new JLabel("Current")); final JTextField current = new JTextField(); circuitPane.add(current); circuitPane.add(new JLabel("Resistance")); final JTextField resistance = new JTextField(); circuitPane.add(resistance);   // Add Header to JFrame /*final JPanel imagePanel = new JPanel(); imagePanel.setBackground(Color.DARK_GRAY); BufferedImage bfimage = ImageIO.read(getClass().getResource( "physicsImg.jpg")); ImageLabel image = new ImageLabel(bfimage); JLabel headerLabel = new JLabel(new ImageIcon(bfimage)); headerLabel.add(image); imagePanel.add(headerLabel); */   // Add button components and header   buttonPanel.add(kinematic); buttonPanel.add(dynamics); buttonPanel.add(power); buttonPanel.add(wave); buttonPanel.add(sound); buttonPanel.add(circuit);   add(mainPanel); //mainPanel.add(imagePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH);   validate();   back.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { for (int i = 0; i < buttonArray.length; i++) { mainPanel.remove(buttonArray[i]); } mainPanel.remove(sidePanel); mainPanel.add(buttonPanel, BorderLayout.SOUTH); //mainPanel.add(imagePanel, BorderLayout.CENTER); mainPanel.revalidate(); mainPanel.repaint(); } });   calculate.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ kinematics.checkJTF(kinematicsJT); try{ float distanceI = Float.parseFloat(dI.getText()); kinematics.setDistanceI(distanceI); float distanceF = Float.parseFloat(d0.getText()); float v0 = Float.parseFloat(velocityI.getText()); float v1 = Float.parseFloat(velocityF.getText()); float vA = Float.parseFloat(velocityAverage.getText()); float aA = Float.parseFloat(accelerationAverage.getText());   } catch(NumberFormatException ex){ JOptionPane.showConfirmDialog(null, "Numbers Only"); } } });   kinematic.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(kinematicPane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(buttonPanel, BorderLayout.WEST); mainPanel.add(sidePanel, BorderLayout.CENTER); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint(); } });   dynamics.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(dynamicsPane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(sidePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.WEST); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint();   float f = Float.parseFloat(force.getText()); float m1 = Float.parseFloat(massOne.getText()); float m2 = Float.parseFloat(massTwo.getText()); float r1 = Float.parseFloat(radiusOne.getText()); float r2 = Float.parseFloat(radiusTwo.getText()); float fN = Float.parseFloat(frictionNormal.getText()); } });   power.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(powerPane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(sidePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.WEST); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint();   float w = Float.parseFloat(work.getText()); float e = Float.parseFloat(energy.getText()); float p = Float.parseFloat(powerP.getText()); float m = Float.parseFloat(mass.getText()); float t1 = Float.parseFloat(timeOne.getText()); float t2 = Float.parseFloat(timeTwo.getText()); float v = Float.parseFloat(velocity.getText()); float pF = Float.parseFloat(powerForce.getText()); } });   wave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(wavePane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(sidePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.WEST); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint(); } });   sound.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(soundPane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(sidePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.WEST); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint(); } });   circuit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { sidePanel.add(circuitPane, BorderLayout.CENTER); sidePanel.add(executePanel, BorderLayout.SOUTH); mainPanel.add(sidePanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.WEST); //mainPanel.remove(imagePanel); mainPanel.revalidate(); mainPanel.repaint();   float volt = Float.parseFloat(voltage.getText()); float cI = Float.parseFloat(current.getText()); float rR = Float.parseFloat(resistance.getText()); } });   } public void checkJTF(JTextField[] a){ for (int i = 0 ; i < a.length ; i++){ if(a[i].getText().isEmpty()){ int unknown = i; kinematics.setUnknown(unknown); } } } }```

Code :

```package physicspractice;   public class Kinematics {   float distanceI, distanceF, velocityI, velocityF, velocityAverage, acceleration, timeF, timeI; int unknown;   public Kinematics() { System.out.println(unknown); //float distance = distanceF - distanceI; //float time = timeF - timeI;   if (unknown >= 0 && unknown <= 6) { switch (unknown) { case 1: velocityF = velocityI + acceleration * (timeF - timeI); case 2: distanceF = distanceI + ((velocityI + velocityF) / 2) * (timeF - timeI); case 3: velocityF = (float) Math.sqrt(velocityI * velocityI) + 2 * acceleration * (distanceF - distanceI); case 4: distanceF = (float) (velocityF * (timeF - timeI) + 0.5 * (acceleration * ((timeF - timeI) * (timeF - timeI)))); case 5: velocityAverage = (distanceF - distanceI) / (timeF - timeI); } } }   public void setDistanceI(float distanceI) { this.distanceI = distanceI; }   public void setDistanceF(float distanceF) { this.distanceF = distanceF; }   public void setVelocityI(float velocityI) { this.velocityI = velocityI; }   public void setVelocityF(float velocityF) { this.velocityF = velocityF; }   public void setVelocityAverage(float velocityAverage) { this.velocityAverage = velocityAverage; }   public void setAcceleration(float acceleration) { this.acceleration = acceleration; }   public void setUnknown(int unknown) { this.unknown = unknown; }   public int getUnknown() { return unknown; }   public double getDistanceI() { return distanceI; }   public double getDistanceF() { return distanceF; }   public double getVelocityI() { return velocityI; }   public double getVelocityF() { return velocityF; }   public double getVelocityAverage() { return velocityAverage; }   public double getAcceleration() { return acceleration; } }```
• May 2nd, 2014, 04:32 PM
Norm
Re: Testing double null
I can not compile the post code without many compiler errors.
Most are due to missing import statements.

Can you make a small, complete program that shows the error. Not 30+ errors
• May 2nd, 2014, 04:36 PM
javaStooge
Re: Testing double null
Quote:

Originally Posted by Norm
I can not compile the post code without many compiler errors.
Most are due to missing import statements.

Can you make a small, complete program that shows the error. Not 30+ errors

I replaced the code with both classes as I have them. And it shows only the one error. hopefully the same for you.
• May 2nd, 2014, 04:42 PM
Norm
Re: Testing double null
See post#18

NOTE: Frame is the name of a Java SE class. It's better not to use names that conflict with Java SE class names.
• May 2nd, 2014, 10:19 PM
javaStooge
Re: Testing double null
Alright I managed to fix the simple issue with the method by adding the static. I'm using a Sys.out to test the check method and it works when I print the value from within the method, but it will not pass the value to the kinematics class....

Cruising down Java Highway hitting every pothole! :-q
• May 2nd, 2014, 10:27 PM
Norm
Re: Testing double null
Quote:

fix the simple issue with the method by adding the static
That doesn't make sense with the code in post#19

Quote:

it will not pass the value to the kinematics class.
Can you explain where the code is trying to pass a value to that class?
• May 2nd, 2014, 10:37 PM
javaStooge
Re: Testing double null
I created the kinematics object from the Kinematics class --- within the checkJTF method (located at very bottom of Frame class)... kinematics.setUnknown

By making the checkJTF method static in the Frame class, am I not able to call it within the class.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last