Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:23+00:00 2026-05-28T01:32:23+00:00

What is wrong with this source code? 1 package Core; 2 3 import java.sql.*;

  • 0

What is wrong with this source code?

 1  package Core;
 2  
 3  import java.sql.*;
 4  
 5  public class Course extends Model {
 6      
 7      protected int id;
 8      protected String title;
 9      
10      public Course(int id) {
11          this.id = id;
12      }
13      
14       public static Course getNew(ResultSet res) {
15          try {
16              Course c;
17              c = new Course(res.getInt("course_id"));
18              return c;
19          } catch(SQLException e) {
20              return null;
21          }
22      }
23      
24      @Override
25      public String toString() {
26          return this.title;
27      }
28  }

I’ve set breakpoints on lines 17, 18 and 11 (mentioned in the order of the execution flow), the object is certainly constructed, yet on line 18 it will return a null instead of the object.

What am I missing?

Please don’t tell me how bad static methods are, I know that. Help me understand why is this happenning and how to fix it.

I’m using netbeans + glassfish + java 7 (openjdk) on linux x64.

Addendum

Ok so the problem may be in the way I’m calling it – I’m using reflection and generics.

The caller looks like this

 1  package Core;
 2  
 3  import java.sql.*;
 4  import java.util.*;
 5  import java.lang.reflect.*;
 6  
 7  /**
 8   * Represents storable elements in a database
 9   */
10  public abstract class Model {
11      /**
12       * this attribute is common to all models of the app
13       * this way we can reuse the same connection for
14       * everything (good or bad, depending on the project's requirements; in
15       * our case it's good - this should be a simple application, no mysql
16       * replication and things like that)
17       */
18      protected static Connection conn = null;
19      
20       public static void setConnection(Connection c) {
21          Model.conn = c;
22      }
23       
24       public static void initStatements() throws SQLException {
25           
26       }
27       
28       protected <T extends Model> HashMap<String, Model> resultsetMap(Class<T> cls, ResultSet res) {
29           HashMap<String, Model> data = new HashMap<String, Model>();
30           Method m;
31           try {
32              m = cls.getMethod("getNew", ResultSet.class);
33              
34           } catch(Exception e) {
35               return null;
36           }
37           
38           
39           try {
40               while(res.next()) {
41                   T obj = (T) m.invoke(null, res);
42                   data.put(obj.toString(), obj);
43               }
44           }
45           catch(Exception e) {
46               return null;
47           }
48           return data;
49       }
50       
51       @Override
52       public abstract String toString();
53       
54       public static Model getNew(ResultSet res) {
55           return null;
56       }
57  }

On line 41 I’m calling the above method getNew(). I end up with nulls in the map, both for the keys and the values.

The method on line 28 is called like this

return this.<Course>resultsetMap(Course.class, res);

Sorry for all the mess, I’ve found the (stupid, as usual) cause: the title of the Course class is left unset.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:32:23+00:00Added an answer on May 28, 2026 at 1:32 am

    If you reach line 18, then it can’t be null. But if you are debugging an older version, or you just don’t reach line 18, it can be that you enter the catch block with an exception.

    So, rule of thumb – don’t discard exceptions. For a start, use ex.printStackTrace() to see it in the console.

    Then try directly return new Course(res.getInt("course_id"));

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java applet code package M257Applet import java.applet.*; import javax.swing.*; import java.awt.*; public class HellowApplet
Whats wrong with this code? -(void) drawRect:(CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); if
Whats wrong with this picture? Model: validates_acceptance_of :terms_of_service, :on => :create, :accept => true,
What's wrong with this C# code? I tried to overload the + operator to
What is wrong with this code. The code is finding the javascript and debug1
Something must be wrong with this code right here: + (UIImage*)captureView:(UIView *)theView { UIGraphicsBeginImageContext(theView.frame.size);
I have a simple Class package chapter10; public class CompilationTest { public static void
Can anyone help me debug this program? The following is server code : package
I keep getting an error with this source code. Does anyone know what im
I have this WxWidgets test source code that compiles, and when run, it shows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.