You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ecs-user@jakarta.apache.org by Thufir <ha...@gmail.com> on 2009/02/27 11:59:22 UTC

null pointer

Would there a be a better approach to this class?

It's just supposed to output a result set.  however, since there has to 
be an update mechanism, I figured that might as well be incorporated as 
well.

One thing I notice is that there's no real need to extend table, so I 
think I'm taking maybe a bad approach.

Also, I'm getting a null pointer error further down, commented in CAPS.  
At first the data is good, then a null value.  Oh, maybe the start of a 
nonexistent column?  Not sure.


import a00720398.util.EnumOfMethods;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ecs.html.*;

public class SimpleResultSetTable extends Table {

    private static final boolean isPretty = true;
    private static final long serialVersionUID = 1L;
    private TR tr = new TR();
    private ResultSet resultSet = null;
    private ResultSetMetaData resultSetMetaData = null;
    private int colCount = 1;
    
    private static Logger logger = Logger.getLogger
(SimpleResultSetTable.class.getName()); //Controller.class.getName()


    private Form getDeleteButtonForm(int id) {
        EnumOfMethods enumVal = EnumOfMethods.D;
        String action = "/Assignment";
        String method = "get";
        Form form = new DeleteButtonForm(action, method, enumVal, id);
        return form;
    }
    
    private void header() throws SQLException {
        for (int i = 1; i <= colCount; i++) {
            TH th = new TH();
            th.setPrettyPrint(isPretty);
            th.addElement(resultSetMetaData.getColumnName(i));
            addElement(th);
        }
    }

    private String getHex(Color color){
        return Integer.toHexString(color.getRGB() & 0x00ffffff);
    }
    
    private Form getElementForm(){
        Form form = new ElementForm();
        return form;
    }
    
    private void data() throws SQLException {
        resultSet.beforeFirst();
        while (resultSet.next()) {
            tr = new TR();
            tr.setPrettyPrint(isPretty);
            int i = resultSet.getRow();
            Color color = (i % 2 == 0) ? Color.LIGHT_GRAY : Color.GRAY;
            String colorString = getHex(color);
            tr.setBgColor(colorString);

            for (int j = 1; j <= colCount; j++) {
                TD td = new TD();
                td.setPrettyPrint(isPretty);
                String element =  resultSet.getString(j);//String.valueOf
(j); //
                if (j == 1) {
                    int id = Integer.parseInt(element);
                    Form form = this.getDeleteButtonForm(id);
                    td.addElement(form);
                } else {

                    //NULL POINTER ERROR RIGHT HERE
                    //I'M NOT SURE WHY ELEMENT IS LITERALLY NULL,
                    //BUT IT IS.  THERE'S GOOD DATA AT FIRST, THEN
                    //A NULL VALUE

                    logger.log(Level.WARNING, "why null?\t" + element);
                   Form form = new ElementForm(element, 1,2);
                   //form.addElement(element);
                    td.addElement(form);
                }
                tr.addElement(td);
                addElement(tr);
            }
        }
    }

    public SimpleResultSetTable(ResultSet rs) throws SQLException {
        setBorder(1);
        setPrettyPrint(isPretty);
        resultSet = rs;
        resultSetMetaData = rs.getMetaData();
        colCount = resultSetMetaData.getColumnCount();
        header();
        data();
    }
}




thanks,

Thufir


---------------------------------------------------------------------
To unsubscribe, e-mail: ecs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: ecs-user-help@jakarta.apache.org


Re: null pointer

Posted by Martin Kurz <in...@martinkurz.com>.
Hi Thufir,

> Would there a be a better approach to this class?
> 
> It's just supposed to output a result set.  however, since there has to 
> be an update mechanism, I figured that might as well be incorporated as 
> well.
> 
> One thing I notice is that there's no real need to extend table, so I 
> think I'm taking maybe a bad approach.

Depends on your preferences. If you're in need of an ecs table 
constructed from a resultset, then i don't see any real problems with 
this approach. Maybe, i would prefer some kind of factory instead, but 
that's my personal opinion.

> Also, I'm getting a null pointer error further down, commented in CAPS.  
> At first the data is good, then a null value.  Oh, maybe the start of a 
> nonexistent column?  Not sure.

Depends on your database: If some columns allow null values, you can of 
course get NULL in a resultset. When you know the type of a cols value 
is int (col 1), you can directly get the int from the resultset by using 
  rs.getInt(j), with rs.wasNull() you can check if the last cols value 
in database was null (you're in need of this check, because when ie 
using getInt(), you don't know if the resulting int 0 is 0 because 
that's the value in database or in database there was null.

---snip---
for (int j = 1; j <= colCount; j++) {
   TD td = new TD();
   td.setPrettyPrint(isPretty);
   if (j == 1) {
       int id = Integer.parseInt(element);
       Form form = this.getDeleteButtonForm(resultSet.getInt(j));
       td.addElement(form);
   } else {
       String element =  resultSet.getString(j);
       if ( resultSet.wasNull() ) {
         td.addElement("-");
       } else {
         Form form = new ElementForm(element, 1,2);
         //form.addElement(element);
         td.addElement(form);
       }
   }
   tr.addElement(td);
   addElement(tr);
}
---snip---

grretings,

Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: ecs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: ecs-user-help@jakarta.apache.org


Re: null pointer

Posted by Thufir <ha...@gmail.com>.
On Sat, 28 Feb 2009 10:41:52 +1030, Zachary Mitchell, BCIS wrote:

> No offense, but I would prefer that this email address not be sent to.
> 
> Please delete references to it,
> 
> and use:
> 
> stargate7thsymbol@live.co.uk

Your header shows, from my end, as:

Path: news.gmane.org!not-for-mail
From: "Zachary Mitchell, BCIS" <za...@internode.on.net>
Newsgroups: gmane.comp.jakarta.ecs.user
Subject: Re:  null pointer

So I think you may have a unintentionally subscribed the wrong address.  
Unless I inadvertently mailed you directly?

> 
> instead.
> 
> Also, I am a source for help with genuine problems/
> 
> code problems.
> 
> I am unfortunately not a source as a debugger!
> 
> :)



Fair enough, no prob :)



-Thufir




---------------------------------------------------------------------
To unsubscribe, e-mail: ecs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: ecs-user-help@jakarta.apache.org


Re: null pointer

Posted by "Zachary Mitchell, BCIS" <za...@internode.on.net>.
No offense, but I would prefer that this email address not be sent to.

Please delete references to it,

and use:

stargate7thsymbol@live.co.uk

instead.

Also, I am a source for help with genuine problems/

code problems.

I am unfortunately not a source as a debugger!

:)




----- Original Message ----- 
From: "Thufir" <ha...@gmail.com>
To: <ec...@jakarta.apache.org>
Sent: Friday, February 27, 2009 9:29 PM
Subject: null pointer


> Would there a be a better approach to this class?
> 
> It's just supposed to output a result set.  however, since there has to 
> be an update mechanism, I figured that might as well be incorporated as 
> well.
> 
> One thing I notice is that there's no real need to extend table, so I 
> think I'm taking maybe a bad approach.
> 
> Also, I'm getting a null pointer error further down, commented in CAPS.  
> At first the data is good, then a null value.  Oh, maybe the start of a 
> nonexistent column?  Not sure.
> 
> 
> import a00720398.util.EnumOfMethods;
> import java.awt.Color;
> import java.sql.ResultSet;
> import java.sql.ResultSetMetaData;
> import java.sql.SQLException;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.apache.ecs.html.*;
> 
> public class SimpleResultSetTable extends Table {
> 
>    private static final boolean isPretty = true;
>    private static final long serialVersionUID = 1L;
>    private TR tr = new TR();
>    private ResultSet resultSet = null;
>    private ResultSetMetaData resultSetMetaData = null;
>    private int colCount = 1;
>    
>    private static Logger logger = Logger.getLogger
> (SimpleResultSetTable.class.getName()); //Controller.class.getName()
> 
> 
>    private Form getDeleteButtonForm(int id) {
>        EnumOfMethods enumVal = EnumOfMethods.D;
>        String action = "/Assignment";
>        String method = "get";
>        Form form = new DeleteButtonForm(action, method, enumVal, id);
>        return form;
>    }
>    
>    private void header() throws SQLException {
>        for (int i = 1; i <= colCount; i++) {
>            TH th = new TH();
>            th.setPrettyPrint(isPretty);
>            th.addElement(resultSetMetaData.getColumnName(i));
>            addElement(th);
>        }
>    }
> 
>    private String getHex(Color color){
>        return Integer.toHexString(color.getRGB() & 0x00ffffff);
>    }
>    
>    private Form getElementForm(){
>        Form form = new ElementForm();
>        return form;
>    }
>    
>    private void data() throws SQLException {
>        resultSet.beforeFirst();
>        while (resultSet.next()) {
>            tr = new TR();
>            tr.setPrettyPrint(isPretty);
>            int i = resultSet.getRow();
>            Color color = (i % 2 == 0) ? Color.LIGHT_GRAY : Color.GRAY;
>            String colorString = getHex(color);
>            tr.setBgColor(colorString);
> 
>            for (int j = 1; j <= colCount; j++) {
>                TD td = new TD();
>                td.setPrettyPrint(isPretty);
>                String element =  resultSet.getString(j);//String.valueOf
> (j); //
>                if (j == 1) {
>                    int id = Integer.parseInt(element);
>                    Form form = this.getDeleteButtonForm(id);
>                    td.addElement(form);
>                } else {
> 
>                    //NULL POINTER ERROR RIGHT HERE
>                    //I'M NOT SURE WHY ELEMENT IS LITERALLY NULL,
>                    //BUT IT IS.  THERE'S GOOD DATA AT FIRST, THEN
>                    //A NULL VALUE
> 
>                    logger.log(Level.WARNING, "why null?\t" + element);
>                   Form form = new ElementForm(element, 1,2);
>                   //form.addElement(element);
>                    td.addElement(form);
>                }
>                tr.addElement(td);
>                addElement(tr);
>            }
>        }
>    }
> 
>    public SimpleResultSetTable(ResultSet rs) throws SQLException {
>        setBorder(1);
>        setPrettyPrint(isPretty);
>        resultSet = rs;
>        resultSetMetaData = rs.getMetaData();
>        colCount = resultSetMetaData.getColumnCount();
>        header();
>        data();
>    }
> }
> 
> 
> 
> 
> thanks,
> 
> Thufir
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ecs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: ecs-user-help@jakarta.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: ecs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: ecs-user-help@jakarta.apache.org