3

I've created a simple gui for my program using Intellij GUI Designer. Everything works fine however when I build gradle jar and try to run it I get NullPointerException (It creates Frame but doesn't see components). I was having a problem configurating build.gradle file so I belive it might be a reason.

Here's build.gradle

group 'ImgScrapper'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'
apply plugin: 'java'




jar {
    manifest {
        sourceSets.main.java.srcDirs = ['Main/src']
        attributes 'Main-Class': 'imgscrapper.Main'
    }
}
sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    compile 'org.jsoup:jsoup:1.9.2'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

And here's catalog tree

 .
 ├── build.gradle
 ├── gradle
 │   └── wrapper
 │       ├── gradle-wrapper.jar
 │       └── gradle-wrapper.properties
 ├── gradlew
 ├── gradlew.bat
 ├── Main
 │   ├── Main.iml
 │   └── src
 │       └── imgscrapper
 │           ├── Frame.form
 │           ├── Frame.java
 │           ├── GetImages.java
 │           └── Main.java
 ├── out
 │   └── production
 │       └── Main
 │           ├── com
 │           │   └── intellij
 │           │       └── uiDesigner
 │           │           └── core
 │           └── imgscrapper
 └── settings.gradle

Error messege

Exception in thread "main" java.lang.NullPointerException
        at imgscrapper.Frame.<init>(Frame.java:34)
        at imgscrapper.Main.main(Main.java:12)

Frame.java file

 public class Frame extends JFrame{
     private JPanel panel;
     private JTextField textField1;
     private JButton button1;
     public JScrollPane scrollPane;
     .
     .
     .

     Frame(){
         setVisible(true);
         setSize(440,290);
         setLocation(450,300);
         setTitle("imgScrapper");
       -/*-> Here's 34 line of Frame.java*/ textField1.setText("What are you looking ./r...");
         comboBox1.setSelectedIndex(1);
         comboBox2.setSelectedIndex(0);

Can you help mi with that ? Thanks

JohnnyGat
  • 325
  • 2
  • 13
  • Can you post the full exception, please? I suspect you may need to reference the intellij UI designer library in your dependencies. – wakjah Jul 17 '16 at 13:15
  • Can you please edit your question to include: 1) the full exception description, and 2) the function containing the line of code that caused the error. – wakjah Jul 17 '16 at 13:30
  • @wakjah Ok, did that – JohnnyGat Jul 17 '16 at 15:17
  • Do you initialize textField1 anywhere? Like textField1 = new JTextField(); ? – Kyle Jul 17 '16 at 18:02

1 Answers1

2

It looks like you never set your member variables (the swing components panel, textField1, etc.) to anything, which means they will be null pointers - hence the exception. I assume that IntelliJ's library is meant to automatically set them up for you at some point.

I have never used IntelliJ designer but it looks like you have to do a few things to use it with a Gradle build script.

First, you have to configure IntelliJ to update your source code via Settings -> GUI Designer -> Generate GUI into Java source code and then add the IntelliJ forms library to your dependencies in the build script

dependencies {
    compile 'com.intellij:forms_rt:7.0.3'
}

This information is taken from Intellij Idea 13 UI Designer and automatic Gradle building - however, I can't vouch for its correctness so you will have to give it a try and see what happens.

Kishan Donga
  • 2,851
  • 2
  • 23
  • 35
wakjah
  • 4,541
  • 1
  • 18
  • 23
  • Thanks a lot ! If somebody is having same problem - dont forget to import forms_rt lib after adding dependency – JohnnyGat Jul 17 '16 at 18:44
  • I had to re-run my project in intellij before it made the source changes. Then I was able to do a build and it worked. – Thom Oct 30 '17 at 17:04