标题:求高手帮忙看下问题出在哪?为什么会报空指针
取消只看楼主
leonardo38
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2009-1-1
结帖率:20%
 问题点数:0 回复次数:0 
求高手帮忙看下问题出在哪?为什么会报空指针
类1:

package common.memcached;

import java.util.Date;  

import com.danga.MemCached.MemCachedClient;  
import com.danga.MemCached.SockIOPool;  
import common.WebLogs;
  
@SuppressWarnings("deprecation")  
public class MemCachedManager {  
    // 创建全局的唯一实例   
    protected static MemCachedClient mcc = new MemCachedClient();  
    protected static MemCachedManager memCachedManager = new MemCachedManager();  
      
    // 设置与缓存服务器的连接池  
    static {   
        // 服务器列表和其权重   
        String[] servers = { "127.0.0.1:11211" };   
        Integer[] weights = { 3 };   
         
        // 获取socke连接池的实例对象   
        SockIOPool pool = SockIOPool.getInstance();   
         
        // 设置服务器信息   
        pool.setServers(servers);   
        pool.setWeights(weights);   
         
        // 设置初始连接数、最小和最大连接数以及最大处理时间   
        pool.setInitConn(5);   
        pool.setMinConn(5);   
        pool.setMaxConn(250);   
        pool.setMaxIdle(1000 * 60 * 60 * 6);   
         
        // 设置主线程的睡眠时间   
        pool.setMaintSleep(30);   
         
        // 设置TCP的参数,连接超时等   
        pool.setNagle(false);   
        pool.setSocketTO(3000);   
        pool.setSocketConnectTO(0);   
         
        // 初始化连接池   
        pool.initialize();   
         
        // 压缩设置,超过指定大小(单位为K)的数据都会被压缩   
//        mcc.setCompressEnable(true);   
//        mcc.setCompressThreshold(64 * 1024);   
    }  
      
    /**   
     * 保护型构造方法,不允许实例化! */   
    protected MemCachedManager() {   
    }  
      
    /**   
     * 获取唯一实例 */   
    public static MemCachedManager getInstance() {   
        return memCachedManager;   
    }  
      
    /**   
     * 添加一个指定的值到缓存中.   
     * @param key   
     * @param value   
     * @return */   
    public boolean add(String key, Object value) {
        return mcc.add(key, value);   
    }   
      
    public boolean add(String key, Object value, Date expiry) {   
        return mcc.add(key, value, expiry);   
    }   
      
    public boolean replace(String key, Object value) {   
        return mcc.replace(key, value);   
    }   
      
    public boolean replace(String key, Object value, Date expiry) {   
        return mcc.replace(key, value, expiry);   
    }  
      
    /**   
     * 根据指定的关键字获取对象.   
     * @param key   
     * @return */   
    public Object get(String key) {   
        return mcc.get(key);   
    }  
      
    public static void main(String[] args)throws Exception {   
        MemCachedManager cache = MemCachedManager.getInstance();   
        cache.add("hello", 234);   
        System.out.print("get value : " + cache.get("hello"));   
    }   
}


类2:
package common.memcached;

import junit.framework.TestCase;

import org.junit.Test;
import org.quartz.utils.weblogic.WeblogicConnectionProvider;

import common.WebLogs;

import dao.dms.system.model.User;


public class TestMemcached extends TestCase {
    private static MemCachedManager cache;         
    @Test      
    public void testCache() {
        WebLogs.getLogger().info("______________________");
   
        User tb = new User();
        tb.setXtczmc("测试数据1");
        WebLogs.getLogger().info("name=开始");
        cache.add("bean", tb);
        User tb1 = (User) cache.get("bean");
        WebLogs.getLogger().info("name1=" + tb1.getXtczmc());
        tb1.setXtczmc("测试数据2");
        tb1 = (User) cache.get("bean");
        WebLogs.getLogger().info("name2=" + tb1.getXtczmc());
    }
   
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        cache = MemCachedManager.getInstance();
    }
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        cache = null;
    }
}


类3:


package common;

import common.memcached.TestMemcached;
 
public class Test {
   

   
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
         TestMemcached tm=new TestMemcached();
         tm.testCache();
        
    }   


}



类3作为JAVA运用程序执行后的结果:
11:41:00:065  INFO (TestMemcached.java:17) - ______________________
11:41:00:070  INFO (TestMemcached.java:21) - name=开始
Exception in thread "main" java.lang.NullPointerException
    at common.memcached.TestMemcached.testCache(TestMemcached.java:22)
    at common.Test.main(Test.java:16)



请问,为什么在调用MemCachedManager的ADD方法会报空指针?求解决方法
搜索更多相关主题的帖子: package public 服务器 import 
2013-12-03 11:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-424614-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.256482 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved