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 7 of 7

Thread: What is wrong with my (beginner) code?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What is wrong with my (beginner) code?

    Hi,

    Can you please tell me what have I done wrong n the following code?
    I'm trying to create a new instance carte of object Carti using the constructor and then to insert a row into a table created with SQL.
    Thank you very much.

    The error I'm getting is:

    Exception in thread "main" java.lang.NullPointerException
    at Carti.Carti.InsertCarti(Carti.java:103)
    at Main.main(Main.java:37)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 28 seconds)

    The line Main.main(Main.java:37) is when I try to insert the row.
    The line Carti.Carti.InsertCarti(Carti.java:103) is when I do the
    PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu"
    + ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");


    Here is the code (Main and Carti Classes)

     
     
     
    import Carti.Carti;
    import Imprumut_Carti.Imprumut_Carti;
    import Membrii.Membrii;
    import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
    import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
    import java.sql.SQLException;
    import java.text.DateFormat;
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         * @throws java.sql.SQLException
         * @throws java.lang.ClassNotFoundException
         * @throws java.lang.IllegalAccessException
         * @throws java.lang.InstantiationException
         */
        public static void main(String[] args) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
            int valid;
            int validd;
            int validdd;
     
            //Pentru Carti
            Carti carte = new Carti(0, " ", " ", " ", " ", 0);
     
            carte.InsertCarti(1, "The Chronicles of Narnia",
            "description", "C.S. Lewis", "HarperCollins", 19);
            carte.InsertCarti(2, "The Lord of the Flies",
            "description", "C.S. Lewis", "Faber&Faber", 19);
        }
     
        package Carti;
     
    import java.sql.*;
     
     
    /**
     *
     */
    public class Carti {
     
       static Connection conn = null;
     
        static void connect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
        {
              Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                  conn = DriverManager.getConnection ("jdbc:mysql://localhost/test", "root", "");
        }
        static String getString(String stt) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
        {
                connect();
                Statement st = conn.createStatement();
                st.executeQuery(stt);
                ResultSet rs = st.getResultSet();
                rs.next();
                String rez = rs.getString(1);
                close();
                return rez;
        }
     
        //Attributes
    char[] form45 = new char[45];
    char[] form256 = new char[256];
     
    public int Id;
    public String titlu = new String(form45);
    public String descriere = new String(form256);
    public String autor = new String(form45);
    public String editie = new String(form45);
    public int anPublicare;
     
    //Constructor
    public Carti(int Id, String titlu, String descriere, String autor, 
            String editie, int anPublicare)
    {
        this.Id = Id;
        this.titlu = titlu;
        this.descriere = descriere;
        this.autor = autor;
        this.editie = editie;
        this.anPublicare=anPublicare;
    }
      //Overrride
    @Override public String toString()
    {
        return "Id-ul cartii este: " + this.Id +
                " Titlul sau este: "+this.titlu + 
                " Descrierea sa: "  + this.descriere +
                " Autorul:  " + this.autor +
                " Editia: " + this.editie +
                " Anul Publicarii: " + this.anPublicare; 
    }
     
     
     //Insert Carti
     public void InsertCarti(int IdCartiVar, String titluVar, String DescriereVar, 
             String AutorVar, String EditieVar, int anPublicareVar) throws SQLException
     {
     
     
       PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu"
               + ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");
       st.setInt(1, IdCartiVar);
       st.setString(2, titluVar);
       st.setString(3, DescriereVar);
       st.setString(4, AutorVar);
       st.setString(5, EditieVar);
       st.setInt(6, anPublicareVar);
       st.execute();  
     
     
     }
    static void close() throws SQLException
        {
            conn.close();
        }
     
     
     }
    Last edited by Miralv; March 30th, 2014 at 08:57 AM. Reason: Reviewing the format of the post


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,781
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: What is wrong with my (beginner) code?

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please post your code correctly per the above link. If you're getting errors, also post those. If not, describe the problems you're having and ask specific questions.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my (beginner) code?

    Thank you for the welcome and for the tips, GregBrannon!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: What is wrong with my (beginner) code?

    Exception in thread "main" java.lang.NullPointerException
    at Carti.Carti.InsertCarti(Carti.java:103)
    At line 103 in the code, there is a null value when the code is executed. Look at line 103, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Miralv (March 30th, 2014)

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my (beginner) code?

    The null variable is conn, which is initialized by
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/test", "root", "");
    The MySQL database I'm trying to connect to is on the localhost, is called test, the username of the connection is root, password null, so I still don't understand what is wrong.
    I do understand this may seem trivial to you... but I've hit a dead-end again. Any ideas?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: What is wrong with my (beginner) code?

    Is that code executed? I don't see how in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my (beginner) code?

    No, it was not executed. Now it is so obvious. Thank you very much

Similar Threads

  1. [beginner] What's wrong with my code?
    By royyoo123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 30th, 2013, 12:28 AM
  2. I dont see anything wrong with this(simple beginner code)
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 26th, 2012, 03:11 PM
  3. [SOLVED] What's wrong with my code?? (Total beginner)
    By TheProf in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 6th, 2012, 01:41 PM
  4. Please held. What is wrong with my code? I am a beginner ;)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 30th, 2011, 04:33 PM
  5. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM