Forum Stats

  • 3,826,778 Users
  • 2,260,707 Discussions
  • 7,897,072 Comments

Discussions

remove ";" at end of String not working

goodluck247
goodluck247 Member Posts: 335
edited Dec 1, 2016 2:40PM in Java Programming

Hello all,

I am new to Java. I have a simple Java program which selects text from an Oracle database table (a Varchar2 column), then prints it to a file.

Here's an extract from the code:

.....

import Java.lang.*;

.....

String recom = null;

String recomf = null;

.....

query = "select recom from recom_logic where labno = " + labno;

rs1 = stmt1.executeQuery(query);

while (rs1.next()){

     recom = rs1.getString("RECOM");

}

if (recom.endsWith(";")){

     recomf = recom.substring(0, recom.length() - 1);

}

.....

the program then goes on to print the data...

the program compiles successfully.

HOWEVER, when running the program, it gives the following error:

Exception in thread "main" java.lang.NullPointerException

I understand it has something to do with my String vars. What am I doing wrong?

Thanks in advance.

goodluck247rpc1

Best Answer

  • rpc1
    rpc1 Member Posts: 1,503
    edited Nov 28, 2016 1:38PM Answer ✓

    import Java.lang.*;

    .....

    String recom = null;

    String recomf = null;

    .....

    query = "select recom from recom_logic where labno = " + labno;

    rs1 = stmt1.executeQuery(query);

    while (rs1.next()){

         recom = rs1.getString("RECOM");

    }

    //you need check recom

    if ((recom!=null)&&(recom.endsWith(";"))){

         recomf = recom.substring(0, recom.length() - 1);

    }

    why your if statement out while ?

    In your code you will get only last  row

«1

Answers

  • rpc1
    rpc1 Member Posts: 1,503
    edited Nov 28, 2016 1:38PM Answer ✓

    import Java.lang.*;

    .....

    String recom = null;

    String recomf = null;

    .....

    query = "select recom from recom_logic where labno = " + labno;

    rs1 = stmt1.executeQuery(query);

    while (rs1.next()){

         recom = rs1.getString("RECOM");

    }

    //you need check recom

    if ((recom!=null)&&(recom.endsWith(";"))){

         recomf = recom.substring(0, recom.length() - 1);

    }

    why your if statement out while ?

    In your code you will get only last  row

  • goodluck247
    goodluck247 Member Posts: 335
    edited Nov 28, 2016 2:21PM

    rpc1, it's a part of code,
    this while loop in within a bigger while loop (while rs.next), so it was specifically disigned like that.

    thanks

  • goodluck247
    goodluck247 Member Posts: 335
    edited Nov 28, 2016 2:30PM

    rpc1, thanks for this:

    //you need check recom

    if ((recom!=null)&&(recom.endsWith(";"))){

         recomf = recom.substring(0, recom.length() - 1);

    }

    I get it now.

  • Unknown
    edited Nov 28, 2016 3:32PM

    this while loop in within a bigger while loop (while rs.next), so it was specifically disigned like that.

    Then it was designed WRONG.

    Well-written code ALWAYS checks that statements complete successfully.

    while (rs1.next()){

    The query may not return any rows at all. Or it could return 1, 2 or a million.

    The code needs to be written to do the right thing no matter WHAT the result set contains.

    You can't just assume the while loop will always return just one row.

    I suggest you fix the code to work properly.

         recom = rs1.getString("RECOM");

    The value retrieved from the DB may be NULL. Guess what that 'getString' method returns if the value is null.

    HOWEVER, when running the program, it gives the following error:Exception in thread "main" java.lang.NullPointerExceptionI understand it has something to do with my String vars. What am I doing wrong?

    One thing you are doing wrong is not using a GUI like NetBeans to debug your code.

    If you did you could step through the code one line at a time and see what it was doing and what results it gave.

    Then you could see what caused the exception and why that variable contained a null.

  • goodluck247
    goodluck247 Member Posts: 335
    edited Nov 29, 2016 12:52PM

    rp0428,

    Then it was designed WRONG.

    Well-written code ALWAYS checks that statements complete successfully.

        - this particular program is supposed to select ALL rows there are, it's a cumulative report printing EVERYTHING, maybe you are taking it out of the context?

    The value retrieved from the DB may be NULL. Guess what that 'getString' method returns if the value is null.

         - so far it prints an empty cell, which is exactly what is required of it

    One thing you are doing wrong is not using a GUI like NetBeans to debug your code.

        - I can't use NetBeans for reasons beyond my control

    otherwise, thank you for your feedback!

  • Unknown
    edited Nov 29, 2016 3:40PM
     - this particular program is supposed to select ALL rows there are, 
    it's a cumulative report printing EVERYTHING, maybe you are taking it
    out of the context?

    Maybe you don't understand what I am saying.

    The code HAS NO EXCEPTION handling. Most of those statements can fail and your code doesn't check for that. It just tries to continue executing no matter what might have happened before.

    Nothing wrong with selecting 'ALL' rows. But 'all' can also mean NO rows or an exception. You need to check for those conditions.

    - I can't use NetBeans for reasons beyond my control

    Then you need to pound on your manager to get approval for a GUI of some sort.

    Nobody develops and tests manually anymore. It isn't possible to test code thoroughly without the assistance of a development environment.

    Many GUIs, including NetBeans, are free to download and use.

    So your manager has no excuse at all for not allowing your devs to use such a tool.

    goodluck247rpc1
  • goodluck247
    goodluck247 Member Posts: 335
    edited Nov 30, 2016 11:26AM

    OK, now I understand.

    I inherited this particular program from previous programmer, who was a Senior programmer by the way.

    In any case, I will look into this code again when I have time, you make perfect sense when you say that there should be exception handling at all times.

    And I can't agree with you more that I should have a GUI at my disposal. They have a production and a test server here, both with Linux, but THEY HAVE NOT UPDATED THE OS IN 6-7 YEARS. They are saying, it's because if they update the systems, they would have to update all Forms, all PRO*C progs, Java, and other stuff as well, so they are not updating... I am not sure if I can install a GUI on a 6-7 year old OS...

    Thanks for your advice in any case!

  • rpc1
    rpc1 Member Posts: 1,503
    edited Dec 1, 2016 1:10AM
    goodluck247 wrote:And I can't agree with you more that I should have a GUI at my disposal. They have a production and a test server here, both with Linux, but THEY HAVE NOT UPDATED THE OS IN 6-7 YEARS. They are saying, it's because if they update the systems, they would have to update all Forms, all PRO*C progs, Java, and other stuff as well, so they are not updating... I am not sure if I can install a GUI on a 6-7 year old OS...

    I think @rp0428  was right about GUI IDE. You could try install Eclipse, Netbeans, JetBrains compatible with your OS version , 6 years it is not much for OS! many companies uses old version of OS, only fixes security issues. But I guess it is not a good idea to install IDE on the server (test or production), because there must be a DEV enviroment, for development. For example you could install IDE and all required frameworks on your desktop and make builds on it, than publish distributives on the TEST server

    goodluck247goodluck247
  • goodluck247
    goodluck247 Member Posts: 335
    edited Dec 1, 2016 1:11PM

    thanks rpc1,

    but this Java program connects the DB, and on the test server the DB is a copy of the production server from yesterday, so it makes sense writing and testing the program there

    and there is no DB on the desktop, so how would I make builds on it without being able to test the program right away?..

  • rpc1
    rpc1 Member Posts: 1,503
    edited Dec 1, 2016 2:11PM

    Do you can connect to the database from your desktop ??? I can't

    You could build program on the desktop without any database and  test it on the test server or

    use in-memory database engine for integration tests

This discussion has been closed.