function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
haruca.kharuca.k 

エラー「System.NullPointerException: Attempt to de-reference a null object」がでてしまう

お世話になっております。

 

http://ameblo.jp/multi-thread/entry-10472746167.html

上記サイトを参考に実装してみたのですが、

Component02Controllerクラスの8行目の「dto.pref = '山梨県';」の箇所で

エラー「System.NullPointerException: Attempt to de-reference a null object」がでてしまいます。

 

dto内のデータを取得するにはどのように実装すれば良いのでしょうか。

エラー原因の分かる方がいらっしゃいましたらご教授頂けると幸いです。

 

以下、私が実装したソースです。

 

○メインコントローラ

public class MainController{
    public final MainDTO dto{set;get;}
    public MainController(){
        dto = new MainDTO();
    }
}

 

○DTOコントローラ

public class MainDTO{
    //セッションに格納したい情報
    public String userName{set;get;}
}

 

○コンポーネントのクラスにDTOを引き受けるコントローラ

public abstract class BaseComponentController{
    public MainDTO dto{set;get;}
}

 

○コンポーネント1用コントローラ

public class Component01Controller extends BaseComponentController{

    public PageReference moveNext(){
        dto.address = 'てすと';      //★エラーが出る箇所
        PageReference nextPage = Page.Page2;
        return nextPage;
    }

}

 

○コンポーネント2用コントローラ

public class Component02Controller extends BaseComponentController{

    public PageReference movePrevious(){
        PageReference nextPage = Page.Page1;
        return nextPage;
    }

}

 

○コンポーネント1を読み込むページ1(Page1)

<apex:page controller="MainController" showHeader="false" standardStylesheets="false" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ページ1</title>
</head>
    <c:Component01 dto="{!dto}" />
</html>
</apex:page>

 

○コンポーネント2を読み込むページ2(Page2)

<apex:page controller="MainController" showHeader="false" standardStylesheets="false" >
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>ページ2</title>
</head>
<c:Component02 dto="{!dto}" />
</apex:page>

 

 

○コンポーネント1(Component01)

<apex:component controller="Component01Controller">
<!-- <apex:attribute name="dto" description="required" assignTo="{!dto}" type="MainDTO" required="true"/> -->//←原因?
<apex:attribute name="dto" description="required" type="MainDTO" required="true"/>
<body>
Component01<br/>
<apex:form >
ユーザ名:<apex:inputText value="{!dto.userName}"/><br/>
<apex:commandButton action="{!moveNext}" value="進む"/>
</apex:form>
</body>
</apex:component>

 

○コンポーネント2(Component02)

<apex:component controller="Component02Controller">
<!-- <apex:attribute name="dto" description="required" assignTo="{!dto}" type="MainDTO" required="true"/> -->//←原因?
<apex:attribute name="dto" description="required" type="MainDTO" required="true"/>
Component02<br/>
<apex:form >
ユーザ名:<apex:inputText value="{!dto.userName}"/><br/>
<apex:commandButton action="{!movePrevious}" value="戻る"/>
</apex:form>
</apex:component>

 

Best Answer chosen by Admin (Salesforce Developers) 
Taiki YoshikawaTaiki Yoshikawa

APIバージョンで影響が出てたんですね。

assignTo属性とname属性の名前を同じにできなくなったとのことなので、別の名前にしてしまうのが一番簡単だと思います。

assignToの方を変更するとApexクラスに影響がでてしまうと思うのでname属性の名称を変更して<c:Component01 dto="{!dto}" />のdto=を変更するのがいいんじゃないかと思いました。

 

イメージ的にはこんな感じです。

 

Component側

<apex:attribute name="dtoClass" assignTo="{!dto}" description="required" type="MainDTO" required="true"/>

 

Page側

<c:Component01 dtoClass="{!dto}" />

 

名称は適当につけてますが、こんな感じでどうでしょうか。

All Answers

Taiki YoshikawaTaiki Yoshikawa

Component01Controller の dto.address = 'てすと'; の箇所でエラーが発生しているとのことで、dtoというのがMainDTOになると思います。このMainDTOでaddress が宣言されていないように思うのですが、関係あったりしないでしょうか。

haruca.kharuca.k

すみません・・・間違えていました。

 

dto.address = 'てすと';      //★エラーが出る箇所

dto.userName= 'てすと';      //★エラーが出る箇所

 

の間違いです>_<コピペミスでした・・・申し訳ないです。

 

 

-------------------------------------------------------------------

コンポーネントの
<!-- <apex:attribute name="dto" description="required" assignTo="{!dto}" type="MainDTO" required="true"/> -->//←原因?
のコメントアウトを外して
<apex:component controller="Component01Controller">を消し、
APIバージョンを26にすると動きました。

APIバージョン27から、assignTo属性とname属性の名前を同じにできなくなったのですね・・・
http://successjp.salesforce.com/pdf/release/spring13_ReleaseNotes.pdf


と言う事は・・・どのように実装したらいいのでしょう・・・

 

Taiki YoshikawaTaiki Yoshikawa

APIバージョンで影響が出てたんですね。

assignTo属性とname属性の名前を同じにできなくなったとのことなので、別の名前にしてしまうのが一番簡単だと思います。

assignToの方を変更するとApexクラスに影響がでてしまうと思うのでname属性の名称を変更して<c:Component01 dto="{!dto}" />のdto=を変更するのがいいんじゃないかと思いました。

 

イメージ的にはこんな感じです。

 

Component側

<apex:attribute name="dtoClass" assignTo="{!dto}" description="required" type="MainDTO" required="true"/>

 

Page側

<c:Component01 dtoClass="{!dto}" />

 

名称は適当につけてますが、こんな感じでどうでしょうか。

This was selected as the best answer
haruca.kharuca.k

迅速な回答ありがとうございます!!!

おっしゃる通りに実装できました!!!!!

 

大変勉強になります。ありがとうございます。m(  )m

Taiki YoshikawaTaiki Yoshikawa

無事にうまくいったみたいで良かったです(^^)