Stanly T
Stanly T

Reputation: 1054

RxJava. Observable.OnSubscribe cannot be resolved to a type

I'm trying to write my first code in RxJava but i've encountered with some error of library import i suppose.

package second.pack;
import io.reactivex.Observable;
public class Main {
    public static void main(String[] args) {
        Observable <String> observable = Observable.create(
// the line below marked as it has an error in Eclipse
/* 7 line */    new Observable.OnSubscribe<String>(){
                    @Override
                    public void call (Subscriber <String> sub){
                      sub.onNext("New Datas");
                      sub.onComplete(); 
                }
            }
    );
}

}

The Error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Observable.OnSubscribe cannot be resolved to a type
    at second.pack.Main.main(Main.java:7)

enter image description here

Please, can anybody help me with this error? Thanks a lot!

Upvotes: 0

Views: 1801

Answers (2)

akarnokd
akarnokd

Reputation: 69997

You are mixing up constructs between 1.x and 2.x. Are you working with an older tutorial? Try this instead:

import io.reactivex.*;

public class ObservableOnSubscribeTest {
    public static void main(String[] args) {
        Observable<String> observable = Observable.create(
                new ObservableOnSubscribe<String>(){
                    @Override
                    public void subscribe(ObservableEmitter<String> sub){
                        sub.onNext("New Datas");
                        sub.onComplete(); 
                    }
                }
            );
    }
}

Upvotes: 5

Ben M
Ben M

Reputation: 1892

Are you sure you have the correct version of RxJava on your classpath? It looks like you're attemping to use RxJava 1.x (io.reactivex.Observable) but Observable.OnSubscribe was added in RxJava 2.x (rx.Observable)

Upvotes: 1

Related Questions