Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Variable Loosing Data?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable Loosing Data?

    I am having some trouble with the below code for my assignment. I am trying to display the
    line
    in the
    JTextArea
    . But for some reason the
    line
    displays blank when called in
    void addComponents()
    Does anyone know how I can go about getting the information from
    line
    in
    public searchProps(int propID)

    import java.awt.Container;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
     
     
    public class searchProps 
    {
        protected String price, area, query, suburb, date, SaleOrRent, strQuery, line="";
        protected int agentID, upid, StreetNum, numBeds, numBaths, numGarages, ownerID, size;
        protected boolean spool;
        PropertyObject PropertyArray[] = new PropertyObject[3];
        JFrame jf;
        JTextArea txtArea = new JTextArea();
         {
            initialFrame();
            addComponents();
        }
     
     public searchProps(int propID) //search using UPID only
       {
           try 
                   {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
                Statement s = conn.createStatement();
     
     
     
                query = ("SELECT * FROM Properties WHERE UPID = "+propID);
     
                // Fetch table
                s.execute(query);
                ResultSet rs = s.getResultSet();
                while((rs!=null) && (rs.next()))
                {
                    upid=rs.getInt(1);
                    StreetNum=rs.getInt(2);
                    suburb=rs.getString(3);
                    area=rs.getString(4);
                    price=rs.getString(5);
                    agentID= rs.getInt(6);
                    numBeds=rs.getInt(7);
                    numBaths=rs.getInt(8);
                    spool=rs.getBoolean(9);
                    numGarages=rs.getInt(10);
                    date=rs.getString(11);
                    ownerID=rs.getInt(12);
                    SaleOrRent=rs.getString(13);
                    size++;
     
     
                  //  line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
                    System.out.println(line);
                }
     
     
     
                // close and cleanup
                s.close();
                conn.close();
                   }
     
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
     
     
     
       }
     
    void initialFrame()
        {
            jf=new JFrame();
            jf.setSize (1300,700);
            jf.setTitle("Property Oracle | Results Page");
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
         void addComponents()
         {
     
             Container con = jf.getContentPane();
             String out = this.getLine();
             con.setLayout(null);
             txtArea.setBounds(20,30,1200,600);
             con.add(txtArea);
             txtArea.setText("UPID\tStreetNum\tSuburb\tArea\tPrice\tAgentID\tBedrooms\tBathrooms\tSwimming Pool\tGarages\tDate\tOwner\tSale/Rent\n");
             System.out.println(line);
     
     
     
         }
     
    }

    Thanks!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Variable Loosing Data?

    The String variable line is initialized to "" (the empty string) and never given another value.
    What did you expect it to print?
    Maybe uncomment the line that assigns a value to the variable?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Variable Loosing Data?

    What do the println() statements print out when the code is executed?
    You should add some id String so you can tell which println printed what. It is sometimes hard to see that a blank line was printed.
      System.out.println("line1="+ line +"<");
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Loosing Data?

    I've tried without initializing line first, then it just prints out null.

    It prints out the data fine in the searchProps method but null when called from addComponents

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Variable Loosing Data?

    Follow the flow of the code. The value of the variable never changes from "" when addComponents is called
    As suggested, initialize the variable to something besides "", like "initial value" and see what happens then

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Variable Loosing Data?

    Why are you using an Initializer block (code in {} outside of method) instead of putting it in a Constructor or method? Add some println statements to the constructor and initializer block to see the execution order.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2013
    Posts
    107
    My Mood
    Amused
    Thanks
    16
    Thanked 10 Times in 10 Posts

    Default Re: Variable Loosing Data?

    You commented out the only line that assigns any data to the line variable. It's not losing data; you never gave it any.

  8. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Loosing Data?

    I have created a LineObject which takes line in as a parameter. I call getLine() from the void addComponents() but returns a null pointer exception?

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Variable Loosing Data?

    Is that a question or a statement with a misplaced question mark?

    If you want help, post the code and the error/stack trace exactly as it occurs at your end, copied and pasted.

  10. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Loosing Data?

    Here's the searchProps class:

    import java.awt.Container;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
     
     
    public class searchProps 
    {
        protected String price, area, query, suburb, date, SaleOrRent, strQuery, out, line="";
        protected int agentID, upid, StreetNum, numBeds, numBaths, numGarages, ownerID, size;
        protected boolean spool;
        PropertyObject PropertyArray[] = new PropertyObject[3];
        LineObject obj;
        JFrame jf;  
        JTextArea txtArea = new JTextArea();
         {
            initialFrame();
            addComponents();
        }
     
    public searchProps(int propID) //search using UPID only
       {
           try 
                   {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
                Statement s = conn.createStatement();
     
     
     
                query = ("SELECT * FROM Properties WHERE UPID = "+propID);
     
                // Fetch table
                s.execute(query);
                ResultSet rs = s.getResultSet();
                while((rs!=null) && (rs.next()))
                {
                    upid=rs.getInt(1);
                    StreetNum=rs.getInt(2);
                    suburb=rs.getString(3);
                    area=rs.getString(4);
                    price=rs.getString(5);
                    agentID= rs.getInt(6);
                    numBeds=rs.getInt(7);
                    numBaths=rs.getInt(8);
                    spool=rs.getBoolean(9);
                    numGarages=rs.getInt(10);
                    date=rs.getString(11);
                    ownerID=rs.getInt(12);
                    SaleOrRent=rs.getString(13);
                    size++;
     
     
                  line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
                  obj= new LineObject(line);
                  System.out.println(line);
                  String out = obj.getLine();
                  System.out.println(out);
                }
     
     
     
                // close and cleanup
                s.close();
                conn.close();
                   }
     
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
     
     
     
       }
        void initialFrame()
        {
            jf=new JFrame();
            jf.setSize (1300,700);
            jf.setTitle("Property Oracle | Results Page");
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
         void addComponents()
         {
     
             Container con = jf.getContentPane();
             con.setLayout(null);
             txtArea.setBounds(20,30,1200,600);
             con.add(txtArea);
             txtArea.setText("UPID\tStreetNum\tSuburb\tArea\tPrice\tAgentID\tBedrooms\tBathrooms\tSwimming Pool\tGarages\tDate\tOwner\tSale/Rent\n");
             out = obj.getLine();
             System.out.println(out);
     
     
     
         }
     
    }

    And Here's the Line Object:


    public class LineObject 
    {
        protected String line;
     
     
        public LineObject(String a)
        {
            line = a;
        }
     
     
        public String getLine()
        {
            return line;
        }
     
    }

    The error I am getting is:
    Exception in thread "main" java.lang.NullPointerException
    at searchProps.addComponents(searchProps.java:488)
    at searchProps.<init>(searchProps.java:19)
    at test.main(test.java:10)

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Variable Loosing Data?

    Respect Java's naming conventions and name your classes with capital letters.

    Where is the variable 'con' initialized?

  12. #12
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Loosing Data?

    in the public searchProps(int propID) class:
    Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
    But that is not the issue

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Variable Loosing Data?

    If you say so. Besides, I was talking about Container con, not Connection conn. 'con' shouldn't need initializing as long as 'jf' has been.

    Somewhere in the method addComponents() is a reference to a null. Add print statements or one that reveals all variables to figure out which it is and then fix it.

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 10:11 AM
  2. Replies: 14
    Last Post: March 6th, 2013, 02:54 PM
  3. how to get a data from database and store it in a variable?
    By jabaman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 03:34 PM
  4. Re-Initialize array without loosing contents..
    By Mr.777 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 17th, 2011, 06:47 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 09:19 AM

Tags for this Thread