• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to accept strings and values in invoice textfields

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i have editted the last post"some help would be nice" and there have been some changes. i thank you so much craig wood and campbell ritchie if either of you are reading this-you are truly excellent ranch-hands.
here is the code and mr.Dittmer i hope this is better to read:

from here to the next break there was no problem in that it compiles but i only see a console window displaying:

AppAccelerator<tm>1.2.010 for Java <JDK 1.2> *86 version
Copyright <c> 1997-1999 Inprise Corporation All Rights Reserved
Exception in thread "main" java.lang.NullPointerException
at Database.<init><Database.java:161>
at Database.main<Database.java:63>




craig made the suggestion that i comment out the following in the beginning since they are already declared as local variables: private JLabel orderID,private JLabel customernCarInfo, private JLabel calculateOrder,private JLabel partsUsed,private Box vertical1, private Box vertical2, private Box vertical3, private Box vertical4, private Box vertical5,private Box vertical6, private DataOutputStream dos,private DataInputStream dis;
i tried that but when i tried to compile it i got the error message that it could not recognize these variables so i didn't do it after all.


this is where the problem starts, i am trying to write the inputs in a file as well as read them. also i want to perform calculations on those fields that recieve numeric values



[ April 12, 2007: Message edited by: natasha henry ]

[ April 12, 2007: Message edited by: natasha henry ]
[ April 12, 2007: Message edited by: natasha henry ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

We're happy to help, but you need to do a couple of things before that help is likely to come. First of all, edit your post to use CODE tags, so that the code formatting stays intact. As it is, the code is basically unreadable: UseCodeTags. Secondly, be specific about what the problem is that you're facing. If there are exceptions during runtime, post the stack trace and indicate in which line they occur. If there are errors during compilations, post those, also with an indication of which line they're in: TellTheDetails
[ April 12, 2007: Message edited by: Ulf Dittmer ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difficult to understand such a large amount of code all at once, but there appear to be errors. I can't get it to compile. Only a few things I can see, but they will probably make a lot of difference.
  • You appear to have a return type for the constructor for DatabaseWindow.
  • There is a lot of code, several hundred lines, after the 2nd { which ought to be inside the constructor.
  • Don't use uneditable JTextFields for "address"; use JLabels.
  • You appear to be declaring the same variables twice, once as fields, then as local variables within that pair of {}. These latter will go out of scope before you reach your constructor. Try putting a test line in that block, like this: "System.out.println("In {} block");//test". See whether you ever reach that block of code at all.
  • Hope this lot makes some difference.
    [ April 12, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Multiple declarations. . . . is followed byYou declare variables called "orderNumber" then you declare variables of the same name inside a block. The latter are local variables, which vanish at the end of the block. Leave the original declarations of fields where they are, move the initialisations into your constructor, and make sure not to declare the same variable twice.

    That should bring about an improvement.

    BTW: Please always use a title for your thread which gives people reading some idea what it is about. And not all upper case, please.
     
    Ranch Hand
    Posts: 1535
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Si. This compiles and shows okay now. See numerous comments in/about trouble areas. Some
    have been mentioned by Campbell above. It will take some more work to liberate the last
    two (commented–out) methods.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . only too pleased to help.

    Craig Wood has provided some code and appears to agree with me.
    Notice he has "commented out" two methods and the .tasha import because some other classes and dependencies were not available.
    It compiles and runs nicely, but I got 21 warnings when I tried it.

    1. WARNING in DBRx.java (at line 7)
    import javax.swing.JButton;
    ^^^^^^^^^^^^^^^^^^^
    The import javax.swing.JButton is never used
    ----------
    2. WARNING in DBRx.java (at line 8)
    import java.awt.event.ActionListener;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The import java.awt.event.ActionListener is never used
    ----------
    3. WARNING in DBRx.java (at line 9)
    import java.awt.event.ActionEvent;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    The import java.awt.event.ActionEvent is never used
    ----------
    4. WARNING in DBRx.java (at line 10)
    import javax.swing.BoxLayout;
    ^^^^^^^^^^^^^^^^^^^^^
    The import javax.swing.BoxLayout is never used
    ----------
    5. WARNING in DBRx.java (at line 15)
    import java.io.DataInputStream;
    ^^^^^^^^^^^^^^^^^^^^^^^
    The import java.io.DataInputStream is never used
    ----------
    6. WARNING in DBRx.java (at line 16)
    import java.io.DataOutputStream;
    ^^^^^^^^^^^^^^^^^^^^^^^^
    The import java.io.DataOutputStream is never used
    ----------
    7. WARNING in DBRx.java (at line 17)
    import java.io.FileOutputStream;
    ^^^^^^^^^^^^^^^^^^^^^^^^
    The import java.io.FileOutputStream is never used
    ----------
    8. WARNING in DBRx.java (at line 18)
    import java.io.FileInputStream;
    ^^^^^^^^^^^^^^^^^^^^^^^
    The import java.io.FileInputStream is never used
    ----------
    9. WARNING in DBRx.java (at line 19)
    import java.io.PrintStream;
    ^^^^^^^^^^^^^^^^^^^
    The import java.io.PrintStream is never used
    ----------
    10. WARNING in DBRx.java (at line 20)
    import java.io.IOException;
    ^^^^^^^^^^^^^^^^^^^
    The import java.io.IOException is never used
    ----------
    11. WARNING in DBRx.java (at line 22)
    public class DBRx extends JFrame
    ^^^^
    The serializable class DBRx does not declare a static final serialVersionUID field of type long
    ----------
    12. WARNING in DBRx.java (at line 31)
    private JLabel label1;
    ^^^^^^
    The field DBRx.label1 is never read locally
    ----------
    13. WARNING in DBRx.java (at line 32)
    private JPanel panel1;
    ^^^^^^
    The field DBRx.panel1 is never read locally
    ----------
    14. WARNING in DBRx.java (at line 65)
    private int count;
    ^^^^^
    The field DBRx.count is never read locally
    ----------
    15. WARNING in DBRx.java (at line 97)
    JLabel label1=new JLabel();
    ^^^^^^
    The local variable label1 is never read
    ----------
    16. WARNING in DBRx.java (at line 166)
    vertical1.add(vertical1.createVerticalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createVerticalStrut(int) from the type Box should be accessed in a static way
    ----------
    17. WARNING in DBRx.java (at line 173)
    vertical2.add(vertical2.createVerticalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createVerticalStrut(int) from the type Box should be accessed in a static way
    ----------
    18. WARNING in DBRx.java (at line 180)
    vertical3.add(vertical3.createHorizontalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createHorizontalStrut(int) from the type Box should be accessed in a static way
    ----------
    19. WARNING in DBRx.java (at line 187)
    vertical4.add(vertical4.createHorizontalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createHorizontalStrut(int) from the type Box should be accessed in a static way
    ----------
    20. WARNING in DBRx.java (at line 194)
    vertical5.add(vertical5.createHorizontalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createHorizontalStrut(int) from the type Box should be accessed in a static way
    ----------
    21. WARNING in DBRx.java (at line 201)
    vertical6.add(vertical6.createHorizontalStrut(25));
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The static method createHorizontalStrut(int) from the type Box should be accessed in a static way
    ----------
    21 problems (21 warnings)[Campbell@dhcppc0 disk-1]$ java DBRx

    About serial version UID: leave that till last; don't do anything about that warning until you have finished everything else.
    The warning about "static way" means you are supposed to call a static method by the name of its class,
    not the name of an instance of that class (an object).
    The warnings about unused and not read mean there are bits of code which you haven't made use of elsewhere;
    presumably those warnings will disappear when you add more Components later.
    Note that warnings don't count as errors; the compiler will still compile the code.

    The application doesn't close. You will have to add some sort of coding to finish off your saving to file
    and then close the app whenever somebody clicks the "X" at the top right of the JFrame.

    [edit this post and next post]Add line breaks to improve display[/edit]
    [ April 13, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . and Craig Wood's amendments seem to get rid of the NullPointerException (NPE).

    NPEs are very common, and we get questions about them on the Ranch all the time.
    I have my own way to deal with an NPE; in 99% of cases they require a change in the coding to correct them.
    Last time I wrote about NPEs was on this thread. Find my first posting.
    [ April 13, 2007: Message edited by: Campbell Ritchie ]
     
    natasha henry
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok mr campbell i am going to work on the event listeners for closing the window and saving the input to file. but i still have a problem-dis.readDouble,dis.readLine,dis.readFloat and dis.readInt is generating the error msgs:
    incompatible types;found: int,required javax.swing.JTextField
    incompatible types;found: float,required javax.swing.JTextField
    incompatible types;found: double,required javax.swing.JTextField
    incompatible types; found: java.lang.String, required javax.swing.JTextField

    could you explain what is going on here
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The problem are all these lines:

    price is a JTextField, not a double (which is what readDouble returns).

    Instead, use
     
    Craig Wood
    Ranch Hand
    Posts: 1535
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Looks like the code comments didn't work very well for you.
    Here is the corrected/suggested gui code:
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Look at the line numbers where you are getting the errors. You are doing something like this:-You ought to have called it customerNameField, rather than customerName. Then it would have been obvious to you that what is on the left of the "=" is a Field.

    Your dis.readLine() method returns a String. You want that String to appear on that field, but you are trying to use the name of the Field as the name of the String. Go through the details of JTextField in the API here, and see iof you can find any way of setting the text of a JTextField (hint: look under where it mentions "JTextComponent").

    I think you can sort out the other errors similarly. You will have to use Integer.toString(i) to turn an int i into a String for the purposes of the display.

    You won't get System.out.println to work with that set of arguments; you want System.out.printf.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I had hoped you would find the setText() method from my hints, but Ulf Dittmer has given you an even heavier hint!
     
    natasha henry
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I JUST LOGGED IN MR CAMPBELL AND OF COURSE I REALIZED WHY THE TEXTFIELDS
    AND COMPILER ALIKE WERE SO STUBBORN-GUESS WHAT,I HAVE MADE SOME IMPRESSIVE
    CHANGES(i think) AND I NEED TO KNOW IF THEY MAKE ANY SENSE. I AM JUST STUMPED AT THE ACTIONEVENT FOR THE SAVE BUTTON. I DON'T KNOW HOW
    TO LINK IT WITH THE REST OF THE CODE.






    [ April 13, 2007: Message edited by: natasha henry ]

    [ April 13, 2007: Message edited by: natasha henry ]

    [ April 13, 2007: Message edited by: natasha henry ]
    [ April 13, 2007: Message edited by: natasha henry ]
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    if (event.getActionCommand() = saveButton)



    Object equality is tested by "=="; what this does is an assignment. What's more, you;re comparing the buttno object to a string, and never set an action command for saveButton.

    You need " saveButton.setActionCommand("save") " where you set up the button, and then the comparison here becomes " if ("save".equals(event.getActionCommand())) "



    /* RIGHT HERE I WANT TO CODE IT IN SUCH A WAY THAT THE SAVE BUTTON AUTOMATICALLY
    ENABLES SAVING OF THE DATA TO THE FILE. HOW DO I CHECK TO ENSURE THAT ALL FIELDS
    REQUIRING TEXT INPUTS ARE COMPLETED BEFORE SUCCESSFULLY CARRYING OUT THE SAVE?*/


    The value in a JTextField can be retrieved as a string via its getText method. Once you have that, you can perform any validation that's needed, parituclarly checking whether the field is empty.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Agree with Ulf Dittmer about checking and validating the Strings from each text field. You can throw an Exception if any has getText().length() == 0.

    Get rid of the addActionListener(this) and delete the actionPerformed() method.

    Add ActionListeners to the individual buttons. It is unfortunate that the Java Tutorials and quite a lot of books use the addActionListener(this) format, which I don't think constitutes good object-oriented programming. I told people about this here, and here, and in lots of other place, I am sure.

    Using addSomethingListener(this) is only really relevant when the event pertains to the displayed object, eg closing a window, position of the mouse pointer on a panel. Otherwise you end up with loads of "if else"s in the actionPerformed method and you end up with a tangled mess of code.

    Campbell's rule of thumb for what to use:-
  • Listener likely to be used in this class and other classes too: public class which implements the WhicheverListener interface
  • Listener likely to be used more than once (or several similar listeners) only in this class: private inner class.
  • Unique listener: nothing else remotely similar in this class: anonymous inner class.
  • I tend not to use Adapters because the tiniest spelling error can lead to it not working, but some people do like them.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you use the JTextField.getText() method you get your values in String format. It is unnecessary to change them to numbers and back, unless you are actually doing some sort of arithmetic with those numbers.

    Passing values from the fields to the database, where you said earlier you are getting Exceptions, I think you are trying to pass the text fields rather than the text they are displaying.

    Don't know much about database connection, I am afraid.

    Please don't try implementing such a large application. Do it bit by bit, one line at a time, then run the application and see what happens.
     
    natasha henry
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you for that pointer mr.campbell-the coding is getting there-its almost at the point of completion despite the hundreds of lines to debug. the only problem i have is that i am still get a nullpointer exception at lines 344 and 65.
    before i even continue though thank you so much craig wood, campbell ritchie and ulf dittmer, you really really really helped. just one more time and i will be out of hair
    SMOOCHES!

    HERE IS WHAT I HAVE NOW COMPILE IT AND TELL ME WHAT YOU THINK

     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Natasha, this is too much code to try to understand by reading through it. If you are getting exceptions, indicate at least on which lines they occur.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have my own ideas about NullPointerExceptions (NPEs) (I have my own ideas about quite a lot of things).
    If you find my first posting in this recent thread, you can read about NPEs.
    Note:-
  • The NPE may not have occurred where you think it has, but the location will be somewhere on the printed stack trace.
  • The posting gives some hints about how to find what the problem is.
  • In 99% of occasions you will need to alter some code to sort out an NPE.
    [edit]Spelling error corrected[/edit]
    [ April 15, 2007: Message edited by: Campbell Ritchie ]
     
    natasha henry
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    they are occurring at lines 344 and 65
    DBrx tester=new DBrx();
    and
    String name= String.valueOf(customerName.getText());
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    No, it can't be on that line.

    Must be the one about BTW: You don't need to put anything about valueOf in that instance. You want to turn it into a String, but the text already is a String. You can simplify it toNow you have three objects on that line which might be null. Can you see all three of them?





































  • 1: name. This is in fact null before you start the line, but it doesn't matter because you are assigning a value to it.
  • 2: customerName. It would have been better to call it customerNameField. Have you initialised it yet?
  • 3: The one you might miss: text. Have you put anything into the text of the Field yet?
  • Have you read the suggestions I made a week ago? Put two lines before where you think the Exception occurs, like this:-See what you get from that.

    Go through the constructors for JTextField and the getText methods and see whether they give you any useful hints.

    [edit] sort out code tags [/edit ]
    [ April 15, 2007: Message edited by: Campbell Ritchie ]
     
    natasha henry
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i am going to do what you recommend but i realize that my java software is very very old because it doesn't have the scanner utility in java io and it doesn't even have printf in its the java PrintStream. i am in the process of downloading another software. i'll be back
    thanks
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Scanner class and the printf method were introduced in Java 5 which was issued in September 2004. I would suggest you download Java 6 update 01.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic