[Qt-jambi-interest] Fwd: Re: NullPointerException under TestNG
José Arcángel Salazar Delgado
arcangel.salazar at gmail.com
Sun Feb 8 19:22:55 CET 2009
El Sunday 08 February 2009 10:28:26 Mathias escribió:
> Gentlemen,
>
> I'm getting a strange crash when instantiating a QAbstractItemModel sub
> class when running a TestNG test.
> Consider this short code example:
>
> import com.trolltech.qt.QtBlockedSlot;
> import com.trolltech.qt.core.QAbstractItemModel;
> import com.trolltech.qt.core.QModelIndex;
> import org.testng.annotations.Test;
>
> public class SimpleTest {
>
> public static class MyModel extends QAbstractItemModel {
> @QtBlockedSlot
> public int columnCount(QModelIndex index) { return 0; }
>
> @QtBlockedSlot
> public Object data(QModelIndex index, int i) { return null; }
>
> @QtBlockedSlot
> public QModelIndex index(int i, int i1, QModelIndex index) { return
> null; }
>
> @QtBlockedSlot
> public QModelIndex parent(QModelIndex index) { return null; }
>
> @QtBlockedSlot
> public int rowCount(QModelIndex index) { return 0; }
> }
>
> @Test
> public void test() {
> System.out.println(new MyModel());
> }
>
> public static void main(String[] args) {
> System.out.println(new MyModel());
> }
>
> }
>
>
> When running the static main() method everything works fine, output is
> something like this:
> test::SimpleTest$MyModel(0x31c92d8)
>
> However, when I run the TestNG test using a TestNG test runner the thing
> crashes somewhere inside the QAbstractItemModel constructor:
>
> java.lang.NullPointerException
> at
> com.trolltech.qt.internal.MetaObjectTools.buildMetaData(MetaObjectTools.jav
>a:787) at
> com.trolltech.qt.core.QAbstractItemModel.__qt_QAbstractItemModel_QObject(Na
>tive Method)
> at
> com.trolltech.qt.core.QAbstractItemModel.<init>(QAbstractItemModel.java:102
>) at
> com.trolltech.qt.core.QAbstractItemModel.<init>(QAbstractItemModel.java:98)
> at test.SimpleTest$MyModel.<init>(SimpleTest.java:25)
> at test.SimpleTest.test(SimpleTest.java:44)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
>9) at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
>l.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
> at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
> at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
> at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
> at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
> at
> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.jav
>a:126) at
> org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110) at
> org.testng.TestRunner.runWorkers(TestRunner.java:720)
> at org.testng.TestRunner.privateRun(TestRunner.java:590)
> at org.testng.TestRunner.run(TestRunner.java:484)
> ...
>
> At first I thought it might be a threading problem, but in this example
> TestNG runs the test on the main thread...
>
> Does anyone have an idea on what might be causing the problem?
> I heavily depend on TestNG for testing my applications and I would hate to
> not be able to use it for testing the non-GUI parts of the apps (like the
> models).
>
> Cheers,
> Mathias
Hello.
The problem (I guess) is that you arent calling QApplication or
QCoreApplication before you try to use a Qt class.
Check this thread. Is a test in Junit, but it can help you.
http://www.mail-archive.com/qt-jambi-interest@trolltech.com/msg00399.html
-------------------------------------------------------
More information about the Qt-jambi-interest
mailing list