-2

私は数週間前に Java を学び始めました。これは PHP から来ており、他の言語のバックグラウンドはありません。私は単に配列()を使用することに慣れています...

クラスを HashMap から LinkedHashMap に変更すると、Null Pointer Exception が発生します。

JDK 1.6 の使用

クラス LinkedHashMap - Oracle Oracle JDK 1.6 リファレンスより

このクラスは、オプションの Map 操作をすべて提供し、null 要素を許可します。HashMap と同様に、ハッシュ関数がバケット間で要素を適切に分散すると仮定すると、基本操作 (追加、保持、および削除) に対して一定時間のパフォーマンスが提供されます。パフォーマンスは、リンクされたリストを維持するためのコストが追加されるため、HashMap のパフォーマンスをわずかに下回る可能性があります。ただし、1 つの例外があります。 . HashMap の反復は、その容量に比例して時間がかかり、よりコストがかかる可能性があります。

動作しません

@Component("blMenu")
public class MenuProcessor extends AbstractModelVariableModifierProcessor {
public  List<Category> topLevelCategories;
public  LinkedHashMap<Category, LinkedHashMap<Integer, List<Category>>> navigationMenu = new LinkedHashMap<Category, LinkedHashMap<Integer, List<Category>>>();
public List<Category> Categories;
public int chunkSize = 5;
public int subCategoriesSize;
public String resultVar;

/**
 * Sets the name of this processor to be used in Thymeleaf template
 */
public MenuProcessor() {
    super("menu");
}

@Override
public int getPrecedence() {
    return 10000;
}

@Override
protected void modifyModelAttributes(Arguments arguments, Element element) {
    CatalogService catalogService = ProcessorUtils.getCatalogService(arguments);

    resultVar = element.getAttributeValue("resultVar");
    String parentCategory = element.getAttributeValue("parentCategory");
    String unparsedMaxResults = element.getAttributeValue("maxResults");

    // TODO: Potentially write an algorithm that will pick the minimum depth category
    // instead of the first category in the list
    List<Category> categories = catalogService.findCategoriesByName(parentCategory);
    if (categories != null && categories.size() > 0) {
        // gets child categories in order ONLY if they are in the xref table and active
        List<Category> subcategories = categories.get(0).getChildCategories();
        if (subcategories != null && !subcategories.isEmpty()) {
            if (StringUtils.isNotEmpty(unparsedMaxResults)) {
                int maxResults = Integer.parseInt(unparsedMaxResults);
                if (subcategories.size() > maxResults) {
                    subcategories = subcategories.subList(0, maxResults);
                }
            }
        }
        topLevelCategories = subcategories;
        buildMenu(arguments, catalogService);
    }
}

public void buildMenu(Arguments arguments, CatalogService catalogService)
{
    LinkedHashMap<Integer, List<Category>> chunkSubs;

    for(Category topLvlCat : topLevelCategories)
    {

        Categories = catalogService.findCategoriesByName(topLvlCat.getName());
        List<Category> subCategories = Categories.get(0).getChildCategories();

        if(subCategories != null && !subCategories.isEmpty())
        {
            subCategoriesSize = subCategories.size();
            chunkSubs = chunkSubCategories(subCategories);
            navigationMenu.put(topLvlCat, chunkSubs);
        }
        else
        {
            navigationMenu.put(topLvlCat, null);
        }
    }
    addToModel(arguments, resultVar, navigationMenu);
}


public LinkedHashMap<Integer, List<Category>> chunkSubCategories(List<Category> subCategories)
{
    List<Category> chunkedCategories;
    LinkedHashMap<Integer, List<Category>> subCategoriesChunked = new LinkedHashMap<Integer, List<Category>>();

    int iLoop = 0;
    int start = 0;
    int end = chunkSize - 1;
    int catSize = subCategoriesSize-1;

    if(subCategoriesSize > 1){
    do
    {

        if(end < catSize)
        {
            chunkedCategories = subCategories.subList(start, end);
            subCategoriesChunked.put(iLoop, chunkedCategories);
        }
        else
        {
            end = end - (end-catSize);
            chunkedCategories = subCategories.subList(start, end);
            subCategoriesChunked.put(iLoop, chunkedCategories);
        }

        iLoop = iLoop + 5;
        start = start + chunkSize;
        end = end + chunkSize;

    }while(subCategoriesSize > iLoop);
    }
    return subCategoriesChunked;
}


}

ただし、すべての LinkHashMaps を HashMaps に変更すると機能します。

スタックトレース

2013 年 3 月 20 日 12:37:46 PM org.apache.catalina.core.StandardWrapperValve 呼び出し SEVERE: パス [/site] のコンテキストでサーブレット [mycompany] の Servlet.service() が例外をスローしました [要求の処理に失敗しました。ネストされた例外は org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'com.mycompany.processor.MenuProcessor' (layout/partials/nav:8)] であり、根本原因は com.mycompany.processor で java.lang.NullPointerException です。 org.broadleafcommerce.common.web.dialect.AbstractModelVariableModifierProcessor.processElement(AbstractModelVariableModifierProcessor.java:46) の com.mycompany.processor.MenuProcessor.modifyModelAttributes(MenuProcessor.java:84) の MenuProcessor.buildMenu(MenuProcessor.java:106) .thymeleaf.processor.element.AbstractElementProcessor.

4

2 に答える 2

0

この奇妙な問題は奇妙になり、すべてのターゲットフォルダーと展開フォルダーを削除し、ビルドなどをやり直しても機能しなくなり、翌日には不思議なことに機能し始めました。

于 2013-03-22T18:48:04.167 に答える
0

この行が本当に nullpointer をスローしている場合:

navigationMenu.put(topLvlCat, null);

その場合、navigationMenu は null です。linkedHashMap と HashMap はどちらも null キーと値を許可するためです。

navigationMenu をフィールドとして定義していることに気付きましたpublic。つまり、どのクラスでも変更できます。これは、フィールドを として宣言しprivate、getter および必要に応じて setter メソッドを提供するという悪い習慣です。

そのため、初期化していますが、他の何かがそれを null に戻している可能性があります。別のクラスまたはテンプレート ファイルで navigationMenu を設定できるものがないことを確認します。

于 2013-03-20T16:59:23.530 に答える