具有类注册的工厂方法模式产生一个空指针异常 [英] factory method pattern with class registration produces a nullpointer exception

查看:412
本文介绍了具有类注册的工厂方法模式产生一个空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在互联网上搜索了这个问题,但在

/factory-pattern.htmlrel =nofollow> http://www.oodesign.com/factory-pattern.html



作者介绍使用反射或对象创建注册课程



我通过以下代码尝试了对象创建方法:
工厂类

  package com.mf.egyptse; 

import java.util.HashMap;

public abstract class ParserFactory {

private static HashMap解析器;

static
{
parsers = new HashMap();

System.out.println(这是第一个静态块);
}


static void putParser(Object key,Object parser)
{
parsers.put(key,parser);

}
static Object getParser(Object key)
{
return parsers.get(key);

}

}

每个解析器注册本身在工厂:
public class NormalParser extends ParserFactory implements ParsingBehavior {

  / ** 
*定义要解析的嵌套列或标签的数量
* /
final static int NO_OF_COLOUMNS = 13;
static String input = null;
static String [] elements = {name,sector,pc,open,close,chgpercent,lastprice,high,low , 音量, 交易, marketcap};



static
{
ParserFactory.putParser(normal,new NormalParser());

}

主要是:

  public class Main {

/ **
* @param args
* @throws IOException
* @throws InterruptedException
* /
public static void main(String [] args)throws IOException,InterruptedException {

// NileParser.parseNile();
// OTCParser.parseOTC();
// NormalParser.parseNormal();


ParsingBehavior parser =(ParsingBehavior)ParserFactory.getParser(normal);



parser.parseToXML(null,null);

}

}

界面是:

  package com.mf.egyptse; 

import java.io.File;
import java.io.IOException;

public interface ParsingBehavior {

void parseToXML(CharSequence input,File file)throws IOException;

}

此代码在执行时总是返回nullpointer异常。该标记是静态块不执行。那么什么是解决方案?

解决方案

正如Snicolas所回答的那样,你的问题是HashMap在被使用的时候没有被填充。主要的静态块应该加载所有必要的解析器类,以便这些类首先注册。

  public class Main {

static {
//加载必要的解析器类
Class.forName(normal);
}

/ **
* @param args
* @throws IOException
* @throws InterruptedException
* /
public static void main(String [] args)throws IOException,InterruptedException {
ParsingBehavior parser =(ParsingBehavior)ParserFactory.getParser(normal);
parser.parseToXML(null,null);
}

}


well, i searched the internet to this problem but didn't find any proper solution

in http://www.oodesign.com/factory-pattern.html

the author described away to register classes using reflection or object creation

i tried the object creation approach by the following code: the factory class

package com.mf.egyptse;

import java.util.HashMap;

public abstract class ParserFactory {

    private static HashMap parsers;

    static
    {
        parsers= new HashMap();

        System.out.println("This is first static block");
    }


    static void putParser(Object key,Object parser)
    {
        parsers.put(key, parser);

    }
    static Object getParser(Object key)
    {
        return parsers.get(key);

    }

}

each parser register itself in the factory: public class NormalParser extends ParserFactory implements ParsingBehavior{

/**
 * Define the number of nested columns or tags to be parsed
 */
final static int NO_OF_COLOUMNS = 13;
static String input = null;
static String[] elements= {"name","sector", "p.c", "open", "close", "chgpercent", "lastprice", "high", "low","value","volume","trades","marketcap"};



static
{
    ParserFactory.putParser("normal", new NormalParser());

}

and the main is :

public class Main {

    /**
     * @param args
     * @throws IOException 
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws IOException, InterruptedException {

//      NileParser.parseNile();
//      OTCParser.parseOTC();
//      NormalParser.parseNormal();


        ParsingBehavior parser = (ParsingBehavior) ParserFactory.getParser("normal");



        parser.parseToXML(null, null);

    }

}

the interface is:

package com.mf.egyptse;

import java.io.File;
import java.io.IOException;

public interface ParsingBehavior {

    void parseToXML(CharSequence input,File file) throws IOException;

}

this code return always nullpointer exception while executing. the porblem is that the static block don't executed. so what is the solution ?

解决方案

As answered by "Snicolas", your problem is that the HashMap is not populated by the time its being used. Your static block in main should load all the necessary parser classes such that these classes register themselves first.

public class Main {

    static {
        // Load necessary parser classes
        Class.forName("normal");
    }

    /**
     * @param args
     * @throws IOException 
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        ParsingBehavior parser = (ParsingBehavior) ParserFactory.getParser("normal");
        parser.parseToXML(null, null);
    }

}

这篇关于具有类注册的工厂方法模式产生一个空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆