Hello Bo,

Thursday, March 15, 2001, 1:31:56 PM, you wrote:

BX> Andrey Myatlyuk wrote:

>> [...]
>> I have my StatesBean loaded by SystemClassLoader. OK.
>> MyServlet by WebAppLoader. Good.
>>
>> So when WebAppLoader reloads my servlet it should ask its parents
>> about loaded class. Ang guess what? StatesBean loaded already with
>> SystemClassLoader. What's wrong with it?
>>
>> Why do I get java.lang.NullPointerException in
>> _statesBean=
>>       getServletContext().getAttribute(StatesBean.STATES_BEAN_NAME);
>>
>> after reloading servlets?
>>
>> --
>> Best regards,
>>  Andrey                            mailto:[EMAIL PROTECTED]

BX> Hi :-)

BX> *  is the following right?
BX>    - StatesBean is your Helper class
BX>    - STATES_BEAN_NAME is a "public static String" object in StatesBean

BX> if(the above is not right)  return;   //haha  ^_^

* yes
* yes + final :)

continue...

BX> *  is StatesBean.STATES_BEAN_NAME.hashCode()  the same one
BX>     before/after MyServlet reloading?
before:
hash code in state servlet=-24382515
hash code in name servlet=-24382515
hash code in city servlet=-24382515

after:
hash code in state servlet=-24382515
hash code in name servlet=-24382515
hash code in city servlet=-24382515


BX> * and I suggest you try the following:
BX>    don't use StatesBean.STATES_BEAN_NAME as the "name" when you
BX>    setAttribute(...):  i.e. I suggest you try:
BX>     - before reloading:
BX>       getServletContext().setAttribute( "haha", new StatesBean() );
BX>     - after reloading:
BX>       Object o = getServletContext().getAttribute( "haha");
BX>       try{
BX>          if(o!=null) StatesBean sb=(StatesBean)o;
BX>       }catch(Exception e){System.out.println(e);}

The same situation. ClassCastException

BX> *  can you post the code of your StatesBean?  and
BX>     the version of TOMCAT? because I also want to
BX>     know why :-)

package com.oneworldinc.flutrack.servlet;

import java.sql.*;
import java.util.*;
import javax.sql.*;
import org.apache.struts.action.*;
import javax.servlet.*;

import com.inet.tds.*;


/**
 * Title:
 * Description:
 * Copyright:       Oneworld Inc. (c) 2001
 * Company:
 * @author          Andrey Myatlyuk
 * @version         1.0
 */

public class StatesBean {

    public static final String STATES_BEAN_NAME = "STATES_BEAN";

    private String  _db_driver;
    private String  _db_url;
    private String  _db_user;
    private String  _db_password;

    private int     _severityLevel;

    private State[] _states;

    private Connection _conn;


    public void init(){


        initDatabaseConnection();

        _states=getStatesWithoutCities();

        for(int a_i=0;a_i<_states.length;a_i++){
            _states[a_i].cities=getCities(_states[a_i].stateID, _severityLevel);
        }


    }



    public void update(){}

    public void releaseResources(){

        try{

            if(_conn != null){
                _conn.close();
            }else{/* ignored */}

            // to null all states

        }catch(SQLException p_exc){
            //
        }

    }


    private void initDatabaseConnection(){


        System.out.println("initDatabaseConnection()");

        System.out.println("Driver="+_db_driver);


        try{
            Class.forName(_db_driver);
        }catch(ClassNotFoundException p_exc){
            System.out.println("Cannot load database driver");
        }

        System.out.println("Driver loaded...");


        try{

            _conn=DriverManager.getConnection(
                _db_url,
                _db_user,
                _db_password
            );


            System.out.println("Connection established...");

        }catch(SQLException p_exc){
            System.out.println("Cannot get database connection");
        }

    }


    private State[] getStatesWithoutCities(){

        State[] a_states=new State[0];

        try{

            String a_sql=
                "SELECT "+
                    "s.StateID,"+
                    "s.StateName,"+
                    "s.StateCode "+
                "FROM "+
                    "States s";

            PreparedStatement a_stmt=_conn.prepareStatement(a_sql);

            ResultSet a_rs=a_stmt.executeQuery();
            ArrayList a_list=new ArrayList(51);

            while(a_rs.next()){

                a_list.add(
                    new State(
                        a_rs.getInt(1),
                        a_rs.getString(2),
                        a_rs.getString(3)
                    )
                );
            }

            int a_size=a_list.size();

            a_states=new State[a_size];

            for(int a_i=0;a_i<a_size;a_i++){
                a_states[a_i]=(State)a_list.get(a_i);
            }

        }catch(SQLException p_exc){
            System.out.println("cannot get empty states");
            System.out.println("message="+p_exc.getMessage());
        }

        return a_states;

    }


    private City[] getCities(int p_stateID, int p_severityLevel){

        City[] a_cities=new City[0];

        String a_sql=
            "SELECT "+
                "hc.StateID,"+
                "hc.City,"+
                "hc.Severity,"+
                "hc.NumberWeeks "+
            "FROM "+
                "HotCities hc "+
            "WHERE "+
                "hc.StateID=? AND "+
                "hc.Severity>=? "+
            "ORDER BY "+
                "hc.City";

        try{

            PreparedStatement a_stmt=_conn.prepareStatement(a_sql);

            a_stmt.setInt(1, p_stateID);
            a_stmt.setInt(2, p_severityLevel);

            ResultSet a_rs=a_stmt.executeQuery();

            ArrayList a_list=new ArrayList(51);


            while(a_rs.next()){

                a_list.add(
                    new City(
                        a_rs.getInt(1),
                        a_rs.getString(2),
                        a_rs.getInt(3),
                        a_rs.getInt(4)


                    )
                );
            }

            int a_size=a_list.size();
            a_cities=new City[a_size];

            for(int a_i=0;a_i<a_size;a_i++){
                a_cities[a_i]=(City)a_list.get(a_i);
            }

        }catch(SQLException p_exc){
            System.out.println("cannot get empty states");
            System.out.println("message="+p_exc.getMessage());
        }

        return a_cities;

    }





    public State[] getStates(){
        return _states;
    }

    public State getStateById(int p_id){

        for(int a_i=0;a_i<_states.length;a_i++){
            if(_states[a_i].stateID == p_id){
                return _states[a_i];
            }else{/* ignored */}
        }

        return null; // just for test - replace by StateNotFoundException

    }




    public void setDriver(String p_database_driver){
        _db_driver = p_database_driver;
    }

    public void setDatabaseURL(String p_database_url){
        _db_url = p_database_url;
    }

    public void setUser(String p_database_user){
        _db_user = p_database_user;
    }

    public void setPassword(String p_database_password){
        _db_password = p_database_password;
    }

    public void setSeverityLevel(int p_severityLevel){
        _severityLevel=p_severityLevel;
    }


}

BX> Bo
BX> Mar.15, 2001



-- 
Best regards,
 Andrey                            mailto:[EMAIL PROTECTED]


Reply via email to