9

मेरे पास एक परियोजना है जो जीयूआई डिजाइनर की मदद से बनाई गई है। यहां मुख्य फॉर्म का कोड है।इंटेलिजे विचार gui डिजाइनर + मेवेन

public class MainForm { 
    MainForm() { 
     directLineOkButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       //some action 
       } 
      } 
     }); 
     crossLineOkButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       //some action 
     }); 
     clearButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      //some action 
     }); 
     cancelButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      //some action 
     }); 
     saveButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      //some action 
     }); 
    } 

    public JPanel getMainPanel() { 
     return mainPanel; 
    } 

    private void createUIComponents() { 
     drawingPanel = new DrawingPanel(); 
    } 

    private JPanel mainPanel; 
    private JComboBox directDirectionCombobox; 
    private JButton directLineOkButton; 
    private JButton crossLineOkButton; 
    private JComboBox crossLineComboBox; 
    private JTextField crossLineSizeValue; 
    private JButton clearButton; 
    private JLabel directLineLabel; 
    private JPanel directLinePanel; 
    private JLabel crossLineLabel; 
    private JPanel crossLinePanel; 
    private JPanel okClearButtonPanel; 
    private JTextField directLineSizeValue; 
    private JButton saveButton; 
    private JPanel drawingPanel; 
    private JButton cancelButton; 
} 

यह ठीक काम करता है। जार फ़ाइल ठीक pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>DOC</groupId> 
    <artifactId>DOC</artifactId> 
    <version>1.0</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.poi</groupId> 
      <artifactId>poi</artifactId> 
      <version>3.9</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <mainClass>Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 

      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>Main</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

के कोड जनरेट करता है, लेकिन जब मैं यहाँ जार फ़ाइल चलाने की कोशिश एक गलती नहीं है।

Exception in thread "main" java.lang.NullPointerException 
at MainForm.<init>(MainForm.java:14) 
at Main.main(Main.java:13) 

ऐसा नहीं है कि गलती दिखाने लाइन जहां directLineOkButton श्रोता बनाई गई है पर है। मैं श्रोताओं को यहां बताया गया है: http://www.jetbrains.com/idea/training/demos/GUI_Designer/GUI_Designer.html cmd + o का उपयोग कर निर्माता में।

<?xml version="1.0" encoding="UTF-8"?> 
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="MainForm"> 
    <grid id="27dc6" binding="mainPanel" layout-manager="FormLayout"> 
    <rowspec value="center:23px:noGrow"/> 
    <rowspec value="top:3dlu:noGrow"/> 
    <rowspec value="center:47px:noGrow"/> 
    <rowspec value="top:3dlu:noGrow"/> 
    <rowspec value="center:max(d;4px):noGrow"/> 
    <rowspec value="top:3dlu:noGrow"/> 
    <rowspec value="center:max(d;4px):noGrow"/> 
    <rowspec value="top:7dlu:noGrow"/> 
    <rowspec value="center:25px:noGrow"/> 
    <rowspec value="top:174dlu:noGrow"/> 
    <rowspec value="center:max(d;4px):noGrow"/> 
    <colspec value="fill:d:noGrow"/> 
    <colspec value="left:4dlu:noGrow"/> 
    <colspec value="fill:452px:noGrow"/> 
    <constraints> 
     <xy x="20" y="20" width="797" height="453"/> 
    </constraints> 
    <properties/> 
    <border type="none"/> 
    <children> 
     <component id="3b663" class="javax.swing.JLabel" binding="directLineLabel"> 
     <constraints> 
      <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties> 
      <text value="Direct Line"/> 
     </properties> 
     </component> 
     <grid id="499e0" binding="directLinePanel" layout-manager="FormLayout"> 
     <rowspec value="center:d:grow"/> 
     <colspec value="fill:98px:noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:80px:grow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <constraints> 
      <grid row="1" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties/> 
     <border type="none"/> 
     <children> 
      <component id="c383d" class="javax.swing.JComboBox" binding="directDirectionCombobox"> 
      <constraints> 
       <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <enabled value="true"/> 
       <model> 
       <item value="Вверх"/> 
       <item value="Вниз"/> 
       <item value="Вправо"/> 
       <item value="Влево"/> 
       </model> 
      </properties> 
      </component> 
      <component id="17aa1" class="javax.swing.JTextField" binding="directLineSizeValue"> 
      <constraints> 
       <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> 
       <preferred-size width="150" height="-1"/> 
       </grid> 
       <forms defaultalign-horz="false"/> 
      </constraints> 
      <properties/> 
      </component> 
      <component id="44fc7" class="javax.swing.JButton" binding="directLineOkButton"> 
      <constraints> 
       <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <text value="OK"/> 
      </properties> 
      </component> 
     </children> 
     </grid> 
     <component id="5a571" class="javax.swing.JLabel" binding="crossLineLabel"> 
     <constraints> 
      <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties> 
      <text value="Cross Line"/> 
     </properties> 
     </component> 
     <grid id="77f1a" binding="crossLinePanel" layout-manager="FormLayout"> 
     <rowspec value="center:d:grow"/> 
     <colspec value="fill:98px:noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:80px:grow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <constraints> 
      <grid row="6" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties/> 
     <border type="none"/> 
     <children> 
      <component id="32368" class="javax.swing.JComboBox" binding="crossLineComboBox"> 
      <constraints> 
       <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <model> 
       <item value="Вверх-вправо"/> 
       <item value="Вверх-влево"/> 
       <item value="Вниз-вправо"/> 
       <item value="Вниз-влево"/> 
       </model> 
      </properties> 
      </component> 
      <component id="dbf23" class="javax.swing.JTextField" binding="crossLineSizeValue"> 
      <constraints> 
       <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> 
       <preferred-size width="150" height="-1"/> 
       </grid> 
       <forms defaultalign-horz="false"/> 
      </constraints> 
      <properties/> 
      </component> 
      <component id="c5c8a" class="javax.swing.JButton" binding="crossLineOkButton"> 
      <constraints> 
       <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <text value="OK"/> 
      </properties> 
      </component> 
     </children> 
     </grid> 
     <grid id="53bbc" binding="okClearButtonPanel" layout-manager="FormLayout"> 
     <rowspec value="center:d:noGrow"/> 
     <colspec value="fill:d:noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <colspec value="left:4dlu:noGrow"/> 
     <colspec value="fill:max(d;4px):noGrow"/> 
     <constraints> 
      <grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties> 
      <enabled value="false"/> 
     </properties> 
     <border type="none"/> 
     <children> 
      <component id="41ba7" class="javax.swing.JButton" binding="saveButton"> 
      <constraints> 
       <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <text value="Сохранить"/> 
      </properties> 
      </component> 
      <component id="a6bf6" class="javax.swing.JButton" binding="clearButton"> 
      <constraints> 
       <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <text value="Очистить"/> 
      </properties> 
      </component> 
      <component id="40f1c" class="javax.swing.JButton" binding="cancelButton"> 
      <constraints> 
       <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> 
       <forms/> 
      </constraints> 
      <properties> 
       <text value="Отменить"/> 
      </properties> 
      </component> 
     </children> 
     </grid> 
     <grid id="2e94e" binding="drawingPanel" custom-create="true" layout-manager="FormLayout"> 
     <rowspec value="center:d:grow"/> 
     <colspec value="fill:d:grow"/> 
     <constraints> 
      <grid row="0" column="2" row-span="11" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> 
      <forms/> 
     </constraints> 
     <properties> 
      <background color="-1"/> 
     </properties> 
     <border type="none"/> 
     <children/> 
     </grid> 
    </children> 
    </grid> 
</form> 

उत्तर

0

आपके गतिशील प्रारंभकर्ता में, आप directLineOkButton एक्सेस कर रहे हैं ({{}} के साथ भाग): यहाँ फार्म के xml का कोड है

directLineOkButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       //some action 
       } 
      } 
     }); 

हालांकि, उस क्षेत्र नहीं है अभी तक असाइन किया गया है, तो आपको एक NullPointerException मिलता है।

+1

हाँ, लेकिन यह फ़ॉर्म के माध्यम से शुरू किया गया है। अगर मैं इसे कन्स्ट्रक्टर में शुरू करता हूं, तो बटन काम नहीं करेगा। –

3

मुझे अपने आप से निर्णय मिला है। http://glxn.net/2010/08/17/making-a-swing-project-using-intellij-idea-and-gui-builder-with-maven-including-executable-jar/ इंटेलिजी विचार gui डिजाइनर के साथ मेवेन परियोजनाओं को बनाने के लिए आपको विशेष प्लगइन की आवश्यकता है।

15

आप ideauidesigner-maven-plugin शामिल करना चाहिए:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>ideauidesigner-maven-plugin</artifactId> 
      <version>1.0-beta-1</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>javac2</goal> 
        </goals> 
       </execution> 
      </executions> 

      <configuration> 
       <fork>true</fork> 
       <debug>true</debug> 
       <failOnError>true</failOnError> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

और वैकल्पिक रूप से - GridLayoutManager या JGoodies अगर आप का उपयोग करें - यह भी forms_rt शामिल निर्भरता के रूप में:

<dependency> 
     <groupId>com.intellij</groupId> 
     <artifactId>forms_rt</artifactId> 
     <version>7.0.3</version> 
    </dependency> 
+0

Thnxx !!!! बहुत – xXxpRoGrAmmErxXx

+2

ध्यान दें कि इस प्लगइन में वर्तमान में एक बग है जो निर्माण को विफल करने का कारण बनती है जैसे '[ERROR] लक्ष्य निष्पादित करने में विफल org.codehaus.mojo: ideauidesigner-maven-plugin: 1.0-beta-1: javac2 (डिफ़ॉल्ट) परियोजना {{project}} पर: लक्ष्य का निष्पादन डिफ़ॉल्ट org.codehaus.mojo: ideauidesigner-maven-plugin: 1.0-beta-1: javac2 विफल: {{some-number}} 'यदि आप अपने स्रोत में लैम्ब्डा अभिव्यक्तियों का उपयोग करते हैं । विवरण के लिए https://stackoverflow.com/questions/32135018/lambda-expressions-and-java-1-8-in-ideauidesigner देखें। – Sumitsu

1

मैं Maven के साथ अपने प्रोजेक्ट के निर्माण के साथ एक ही समस्या थी । "समस्या | सेटिंग्स | संपादक | संपादक | जीयूआई डिजाइनर में मिली" सेटिंग्स में "बाइनरी क्लास फाइल" -> "जावा स्रोत कोड" से "जीयूआई जेनरेट करें:" के लिए मूल्य बदलने के लिए मेरी समस्या का हल क्या हुआ।

+0

यह एक सभ्य कामकाज है जब तक कोई प्लगइन को ठीक नहीं करता है। "उत्पन्न जीयूआई" कॉन्फ़िगर करें कि @code_eater इंगित करता है, फिर इंटेलिजे से मैन्युअल रूप से प्रोजेक्ट बनाएं, जब भी आप इसके लिए जावा स्रोत को पुन: उत्पन्न करने के लिए GUI फ़ॉर्म बदलते हैं। जेनरेट स्रोत को स्रोत नियंत्रण में जांचें (वैकल्पिक रूप से, यदि आप स्रोत नियंत्रण का उपयोग कर रहे हैं), ताकि परियोजना पर सहयोग करने वाला कोई भी मैन्युअल चरण छोड़ सके। फिर मैवेन को स्रोत पेड़ पर सामान्य रूप से चलने दें (अब इंटेलिजे के जेनरेट कोड सहित)। एकमात्र पकड़ यह है कि आपको प्राथमिक निर्भरता के रूप में 'com.intellij: form_rt: 7.0.3' जोड़ना पड़ सकता है। – Sumitsu

संबंधित मुद्दे