加载Null指针加载计算机中的属性文件时出现异常

时间:2016-11-23 15:34:14

标签: java

这是我写的代码。但是当我试图打印键并将其值变为空指针时。

我主要关心的是从属性文件中加载键和值,并需要存储在地图中

Map<String, String> prop1 = new HashMap<String, String>();
Map<String, String> prop2 = new HashMap<String, String>();

public static void main(String args[]) {

    Sample sample = new Sample();
    sample.getLocaleOne();
}

public void getLocaleOne() {
    Properties prop = new Properties();
    InputStream input = null;

    try {
        File file = new File("Labels.properties");
        System.out.println("file.getAbsolutePath());
        input = this.getClass().getClassLoader().getResourceAsStream("C:/workspaceNewI18NChnages/customer/Labels.properties");

        prop.load(input);

        Enumertion<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            System.out.println("Key : " + key + ", Value : " + value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

因为您有属性文件的绝对路径,所以您应该使用const { Provider, connect } = ReactRedux; const { createStore, bindActionCreators } = Redux; // Component let Counter = ({ value, onIncrement, onDecrement }) => ( <div> <p>{ value }</p> <div> <button onClick={ onIncrement }>+</button> <button onClick={ onDecrement }>-</button> </div> </div> ); // Reducer const counter = (state = { counter: 0 }, action) => { switch (action.type) { case 'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; default: return state; } } // actions const onIncrement = () => { return { type: 'INCREMENT' } } const onDecrement = () => { return { type: 'DECREMENT' } }; // map state and actions const mapStateToProps = (state) => { return { value: state.counter } } const mapDispatchToProps = (dispatch) => { return bindActionCreators( { onIncrement, onDecrement }, dispatch ); } Counter = connect( mapStateToProps, mapDispatchToProps )(Counter); const store = createStore(counter); ReactDOM.render( <Provider store={ store }> <Counter /> </Provider>, document.getElementById('container') );,而不是资源流:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.6/react-redux.js"></script>
<div id="container"></div>

答案 1 :(得分:0)

如果你想阅读一个属性文件,只需使用这样的java.util.ressourceBundle:

ResourceBundle bundle = ResourceBundle.getBundle("properties.login");
System.out.printLn(bundle.getString("login"));// read login property
System.out.printLn(bundle.getString("pwd"));//   read pwd property

在这种情况下,login.properties文件位于包属性