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

Thread: what s wrong with my code

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

    Unhappy what s wrong with my code

    hi every bady
    my english is bad sorry
    so, i have a problem in implement method named viewAllFactor(User us,ResultSet rs)
    when i call this method using <T> like objet dont work because T obj is null
    i ask if i can try something to have a good result
    i have 2 classes BaseDao<T> (DAO Pattern) and Class User extends from BaseDao
    this is the code
    thiks
    package appweb.core;
    public abstract  class BaseDao<T> {
    public ArrayList<T> viewall() {
    			ResultSet rset = null;
    			ArrayList<T> listAll = new ArrayList<T>();
    			String sql = "SELECT* FROM "+ this.tableName;
    			System.out.println(sql);
    			try {		
     
    				PreparedStatement ps =  this.connect.prepareStatement(sql);
    				rset = ps.executeQuery(sql);
    				while (rset.next()) {
    					T obj ; [B][COLOR="#A52A2A"](mast initialis obj with null bath when i do that it s wrong in the call method viewAllFactor(obj,rset) [/COLOR][/B]
    					this.viewAllFactor(obj,rset);
    					listAll.add(obj);
    				}
    				rset.close();
    				ps.close();
     
     
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    					return listAll;
    		}
    public class UserDao extends BaseDao<User> {
    .....
    public void viewAllFactor(User u,ResultSet rs) {
     
    	try{
     
    			u.setId(rs.getInt("id"));                                [COLOR="#800000"]  error hier[/COLOR]
    			u.setName(rs.getString("name"));
    			u.setLogin(rs.getString("login"));
    			u.setPass(rs.getString("pw"));
    			u.setIdpr(rs.getInt("idProfil"));
    			}
    		catch (SQLException e) {e.printStackTrace();} 
     
    }
    sorry for my english
    Last edited by essayd100; January 14th, 2014 at 01:20 PM. Reason: change style


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what s wrong with my code

    Please read this topic for info useful for newcomers.

    Don't expect an answer until your code is posted correctly, you've posted a complete error message (the whole thing copied and pasted), and you've asked a specific question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: what s wrong with my code

    Quote Originally Posted by essayd100 View Post
    so, i have a problem in implement method named viewAllFactor(User us,ResultSet rs)
    when i call this method using <T> like objet dont work because T obj is null
    i ask if i can try something to have a good result
    i have 2 classes BaseDao<T> (DAO Pattern) and Class User extends from BaseDao
    From the posted code, I understand that you are trying to do a "generic" select-all logic. Generic in the sense that, being parameterized (mainly by the tableName), it should ideally work for any table/type.

    It's a good idea in general, but there is a particular issue you need to resolve, and in the right way. At some point you must instantiate an object of a concrete class. User is the type you need at this moment, but you could have other "value" classes, eg. Product, Person, etc.

    Where is the instantiation of the User?

    You can't do new T(); in viewall() because T is just only a "placeholder". You need to delegate the instantiation of User (or any other type) to some code that is not known by viewall().

    When there are these type of problems, you need to analyze the problem trying to "see" what changes and what doesn't change.
    Your viewall() executes an "algorithm" (in general): creating the SQL string, preparing the statement, execute the statement, iterating on the result set, etc... This doesn't change. It's the same for any table/type to query. What changes is the concrete class to use and the get of all the values, because this depends on the table/type.

    It seems you have understood this concept, because I see an abstract class BaseDao and a concrete class UserDao. So you are on the right way! You just need to refine the concept.

    In BaseDao you should have an abstract method:

    protected abstract T viewAllFactor(ResultSet rs);

    In any concrete subclass, eg. in UserDao you can have the concrete method:

    protected User viewAllFactor(ResultSet rs) { ..... }

    And in the body you just need to: 1) instantiate the User, 2) use getXyz on ResultSet to get values to assign to User, 3) return the User object.
    In viewall you receive the object and add it to the list.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    Lightbulb Re: what s wrong with my code

    thinks every bady
    i have found solution
    i make a methode to inisialize the objet and i kall it in my Class ado
    thinks

Similar Threads

  1. What Is Wrong With This Code
    By Camwarp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2013, 03:37 PM
  2. What's wrong with my code?
    By techflyer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 13th, 2013, 02:54 PM
  3. What is wrong with this code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 24th, 2012, 12:31 AM
  4. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  5. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM