使用setProperty()方法遇到的一个小问题

本文深入探讨了在使用Java的Properties类时遇到的NullPointerException异常,详细解析了异常产生的原因,即在调用setProperty方法时传入了null值。文章通过分析Hashtable类的put方法源代码,揭示了异常触发的具体机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天调试程序时,在调用Properties类的setProperty(String key, String value)方法时,遇到了一个小问题,程序运行到该语句时抛出异常,提示java.lang.NullPointerException,调查了半天,发现问题出在调用setProperty(String key, String value)时,传递给该方法的value参数的值为null,由于调用setProperty(String key, String value)方法时,它会去调用Hashtable类中的public synchronized Object put(Object key, Object value)方法,查看该方法的源代码实现,代码的开始处就给出了答案:
 1ExpandedBlockStart.gifContractedBlock.gifpublic synchronized Object put(Object key, Object value) {
 2InBlock.gif    // Make sure the value is not null

 3ExpandedSubBlockStart.gifContractedSubBlock.gif    if (value == null{
 4InBlock.gif        throw new
 NullPointerException();
 5ExpandedSubBlockEnd.gif    }

 6InBlock.gif
 7InBlock.gif    // Makes sure the key is not already in the hashtable.

 8InBlock.gif    Entry tab[] = table;
 9InBlock.gif    int hash =
 key.hashCode();
10InBlock.gif    int index = (hash & 0x7FFFFFFF%
 tab.length;
11ExpandedSubBlockStart.gifContractedSubBlock.gif    for (Entry e = tab[index] ; e != null ; e = e.next) 
{
12ExpandedSubBlockStart.gifContractedSubBlock.gif        if ((e.hash == hash) && e.key.equals(key)) 
{
13InBlock.gif        Object old =
 e.value;
14InBlock.gif        e.value =
 value;
15InBlock.gif        return
 old;
16ExpandedSubBlockEnd.gif        }

17ExpandedSubBlockEnd.gif    }

18InBlock.gif
19InBlock.gif    modCount++
;
20ExpandedSubBlockStart.gifContractedSubBlock.gif    if (count >= threshold) 
{
21InBlock.gif        // Rehash the table if the threshold is exceeded

22InBlock.gif        rehash();
23
InBlock.gif
24InBlock.gif            tab =
 table;
25InBlock.gif            index = (hash & 0x7FFFFFFF%
 tab.length;
26ExpandedSubBlockEnd.gif    }
 
27
InBlock.gif
28InBlock.gif    // Creates the new entry.

29InBlock.gif    Entry e = new Entry(hash, key, value, tab[index]);
30InBlock.gif    tab[index] =
 e;
31InBlock.gif    count++
;
32InBlock.gif    return null
;
33ExpandedBlockEnd.gif    }

就此,问题的根源找到了,以后写程序的时候得多注意这些细节。以下附上setProperty(String key, String value)方法的描述:
 1None.gifObject java.util.Properties.setProperty(String key, String value)
 2None.gifCalls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for
 property keys and values. The value returned is the result of the Hashtable call to put. 
 3
None.gif
 4
None.gifSee Also:
 5
None.gifgetProperty

 6
None.gifParameters:
 7None.gifkey: the key to be placed into this
 property list.
 8
None.gifvalue: the value corresponding to key.

 9
None.gifReturns:
10None.gifthe previous value of the specified key in this property list, or null if
 it did not have one.

11
None.gifSince: 1.2

转载于:https://www.cnblogs.com/xxpal/articles/837400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值